nig*_*x79 2 regex perl awk sed
我有一些巨大的文件,其值由管道 (|) 符号分隔。我们引用的字符串,但有时引用的字符串之间有换行符。
我需要使用 Oracle 的外部表读取这些文件,但在换行符上他会给我错误。所以我需要用空格替换它们。
我对这些文件执行了一些其他 perl 命令来解决其他错误,所以我想在一行 perl 命令中找到一个解决方案。
我在 stackoverflow 上发现了一些其他类似的问题,但它们的作用并不完全相同,而且我无法使用那里提到的解决方案找到我的问题的解决方案。
我尝试过但不起作用的声明:
perl -pi -e 's/"(^|)*\n(^|)*"/ /g' test.txt
Run Code Online (Sandbox Code Playgroud)
示例文本:
4454|"test string"|20-05-1999|"test 2nd string"
4455|"test newline
in string"||"test another 2nd string"
4456|"another string"|19-03-2021|"here also a newline
"
4457|.....
Run Code Online (Sandbox Code Playgroud)
应该变成:
4454|"test string"|20-05-1999|"test 2nd string"
4455|"test newline in string"||"test another 2nd string"
4456|"another string"|19-03-2021|"here also a newline "
4457|.....
Run Code Online (Sandbox Code Playgroud)
听起来你想要一个 CSV 解析器Text::CSV_XS(通过操作系统的包管理器或最喜欢的 CPAN 客户端安装):
$ perl -MText::CSV_XS -e '
my $csv = Text::CSV_XS->new({sep => "|", binary => 1});
while (my $row = $csv->getline(*ARGV)) {
$csv->say(*STDOUT, [ map { tr/\n/ /r } @$row ])
}' test.txt
4454|"test string"|20-05-1999|"test 2nd string"
4455|"test newline in string"||"test another 2nd string"
4456|"another string"|19-03-2021|"here also a newline "
Run Code Online (Sandbox Code Playgroud)
该单行代码使用|字段分隔符而不是普通逗号来读取每条记录,并且对于每个字段,用空格替换换行符,然后打印出转换后的记录。