我有一个相对较大的 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/