【发布时间】:2019-05-15 12:33:14
【问题描述】:
所以我写了一个脚本来比较两个文件
所以我的文件是 文件1:
This is line 1.
This is line 2.
This is line 3.
This is line 4.
This is line 5.
文件 2:
this is line 1,aaa
this is line 2,bbb
this is line 3,ccc
所以我的代码所做的是查找逗号前面的句子(在 file2 中)是否存在于文件中,如果存在,则将其替换为逗号后面的句子。
这是我的代码
awk -F'"(,")?' '
NR==FNR { r[$2] = $3; next }
{ for (n in r) gsub(n, r[n]) } 1' file2.csv file1.csv>output.csv
所以我的 output.csv 应该如下所示:
aaa
bbb
ccc
This is line 4.
This is line 5.
只要文件和空格中的大小写不匹配,此代码就可以正常工作。 所以在比较时,我希望它以不区分大小写的方式进行比较并修剪空格。 例如:
When comparing:
file1:
thisisline1.
thisisline2.
thisisline3. etc
并且输出应该是原始格式
This is line 1.
我正在寻找的是即时修剪和小写转换
编辑:使这个问题关于修剪部分更清楚。 我已经写好了代码:
cat file2.csv|tr -s ' '>file3.csv
它的作用是将多个空格合并为一个 所以
This is line 1.
与
相同This is line 1.
但是如果存在带有一个或多个空格的空行,就会出现问题 例如:
this is line 1,aaa
this is line 2,bbb
(blank line but with space)
this is line 3,ccc
this is line 4.
this is line 5.
所以当我先使用我的 awk 命令然后使用修剪功能时,它会失败。 所以即使有了这个文件,我的输出也应该是
aaa
bbb
ccc
This is line 4.
This is line 5.
【问题讨论】:
-
能否在您的帖子中提及更清晰的预期输出。
-
您的样本输入不足以测试解决方案是否有效。您应该包括 RE 元字符和反向引用字符以及作为其他行子集的行,部分匹配的行,包含逗号的行,以及仅在空格中不同的行,但一些有助于分离工作的测试用例来自非工作的“解决方案”。