1

I am trying to add a formatted column between columns of multiple text files. By using

awk 'NR==FNR{c[NR]=$3;l=NR;next}{$2=($3+c[1])*c[l]" "$2}7' file file

I can convert a file that has a form

1 2 3 4
10 20 30 40 
100 200 300 400

to

1 1800 3 4
10 9900 30 40
100 90900 300 400

How can I do above operation to multiple .dat files?

4

1 回答 1

1
tmp="/usr/tmp/tmp$$"
for file in *
do
    awk '...' "$file" "$file" > "$tmp" && mv "$tmp" "$file"
done

写你的脚本,虽然:

awk 'NR==FNR{c[NR]=$3;l=NR;next}{$2=($3+c[1])*c[l]" "$2}7' file file

永远不要使用字母l(el) 作为变量名,因为它看起来太像数字1(one)。我实际上会把它写成:

awk 'NR==FNR{c[++n]=$3;next}{$2=($3+c[1])*c[n]" "$2}7' file file

或者如果内存是大文件的问题:

awk 'NR==FNR{c[NR==1]=$3;next}{$2=($3+c[1])*c[0]" "$2}7' file file
于 2013-07-25T07:43:06.913 回答