我要:
awk
或sed
将列值替换为某些行值File1.csv:
A,B,C,D
E,F,G,H
File2.csv:
XYZ, 123
QWE, 456
要替换:
要获得如下输出:
A,XYZ,123,D
E,XYZ,123,D
必须对多个文件执行此操作,第二个文件如下所示:
R,W,O,P
S,D,I,L
将进行如下更改...
R,QWE,456,P
S,QWE,456,L
让我知道如何使用awk
并循环进行此操作
我尝试了以下操作,但没有成功(在awk
中):
awk 'FNR==NR{a[NR]=$1;next}{$2=a[FNR]}1' file2.csv file1.csv
我还尝试在文件目录中循环运行此awk
:
for file in transmaster*.csv
do
echo $file
awk 'FNR==NR{a[NR]=$1;next}{$2=a[FNR]}1' file2.csv file1.csv
done
...但出现以下错误:
awk: fatal: cannot open file `file' for reading (No such file or directory)
答案 0 :(得分:0)
可以请您尝试以下操作:
awk '
FNR==NR{
a[FNR,1]=$1
a[FNR,2]=$2
next
}
{
$2=a[FNR,1]
$3=a[FNR,2]
}
1' FS=", " file2.csv FS="," OFS="," file1.csv