所以我写了一个脚本,在其中比较两个文件
所以我的文件是 文件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命令,然后使用trim函数时,此操作将失败。 所以即使有了这个文件,我的输出也应该是
aaa
bbb
ccc
This is line 4.
This is line 5.
答案 0 :(得分:1)
请选中此选项
awk -F'"(,")?' '
NR==FNR { r[$2] = $3; next }
{ for (n in r) gsub(tolower(n), tolower(r[n])) } 1' file2.csv file1.csv>output.csv
您也可以使用
IGNORECASE=1
答案 1 :(得分:0)
如果您正在考虑使用Perl,则可以使用以下内容
> cat anuj_f2
this is line 1,aaa
this is line 2,bbb
this is line 3,ccc
> cat anuj_f1
This is line 1.
This is line 2.
This is line 3.
This is line 4.
This is line 5.
> perl -F"," -lane ' $kv{lc $F[0]."."}=$F[1] if $ARGV eq "anuj_f2"; if( $ARGV eq "anuj_f1" ) {s/$_/$kv{lc $_}?$kv{lc $_}:$_/ige;print } ' anuj_f2 anuj_f1
aaa
bbb
ccc
This is line 4.
This is line 5.
>
答案 2 :(得分:0)
$ cat tst.awk
{
key = tolower($0)
gsub(/[[:space:]]+/," ",key)
}
NR==FNR {
val = $0
sub(/,[^,]+$/,"",key)
sub(/.*,/,"",val)
key2val[key"."] = val
next
}
{ print (key in key2val ? key2val[key] : $0) }
$ awk -f tst.awk file2 file1
aaa
bbb
ccc
This is line 4.
This is line 5.