regex - 如何有效地搜索/替换大型 txt 文件?

标签 regex sql-server-2005 perl csv grep

我有一个相对较大的 csv/文本数据文件 (33mb),我需要进行全局搜索并替换其中的分隔符。 (原因是似乎没有办法让 SQLServer 在表导出期间转义/处理数据中的双引号,但那是另一回事了......)

我在较小的文件上成功完成了 Textmate 搜索和替换,但在这个较大的文件上却令人窒息。

似乎命令行 grep 可能是答案,但我不太理解语法,唉:

grep -rl OLDSTRING . | xargs perl -pi~ -e ‘s/OLDSTRING/NEWSTRING/’

所以在我的例子中,我正在搜索“^”(插入符号)字符并替换为“”(双引号)。

grep -rl " grep_test.txt | xargs perl -pi~ -e 's/"/^'

那是行不通的,我假设它与转义双引号或其他东西有关,但我很迷茫。帮助任何人?

(我想如果有人知道如何让 SQLServer2005 在导出到 csv 期间处理文本列中的双引号,那将真正解决核心问题。)

最佳答案

您的 perl 替换似乎是错误的。尝试:

grep -rl \" . | xargs perl -pi~ -e 's/\^/"/g'

解释:

grep : command to find matches
-r : to recursively search
-l : to print only the file names where match is found
\" : we need to escape " as its a shell meta char
. : do the search in current working dir
perl : used here to do the inplace replacement
-i~ : to do the replacement inplace and create a backup file with extension ~
-p : to print each line after replacement
-e : one line program
\^ : we need to escape caret as its a regex meta char to mean start anchor

关于regex - 如何有效地搜索/替换大型 txt 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3549207/

相关文章:

javascript - Java中基于动态正则表达式的字符串替换

perl - 相当于 Perl 中的 >nul 2>&1 (来自 Batch)?

perl - 将日期字符串解析为毫秒,反之亦然

sql-server-2005 - SQL 服务器 2005 : Why would a delete from a temp table hang forever?

mysql - 连接返回错误结果的表

linux - 我如何使用 Perl 调试器的 *supported* 编辑器?

javascript - Google Apps 脚本/Javascript 搜索并替换为正则表达式不起作用

javascript - 无法正确设置数字格式

regex - extjs 输入文本 maskRe

sql-server - SQL Server 中的最大数据库名称长度