我有以下文件,
total.txt
order1,5,item1
order2,6,item2
order3,7,item3
order4,6,item4
order8,9,item8
changed.txt
order3,8,item3
order8,12,item8
total.txt是总订单数据并且已更改.txt是最近更改的数据。我想将最近的变化与总和合并,我希望输出为,
Output.txt的
order1,5,item1
order2,6,item2
order3,8,item3
order4,6,item4
order8,12,item8
注意:total.txt的第(3& 5th)行的第2列用changed.txt文件更新
我已使用下面的nawk
来比较第一个coulmn,但无法将其打印到输出文件。请帮助完成以下命令
nawk -F"," 'NR==FNR {a[$1]=$2;next} ($1 in a) "print??"' total.txt changed.txt
答案 0 :(得分:3)
另一个awk
awk -F, 'FNR==NR {a[$1]=$0;next} {print (a[$1]?a[$1]:$0)}' changed.txt total.txt
order1,5,item1
order2,6,item2
order3,8,item3
order4,6,item4
order8,12,item8
它是如何运作的?
awk -F, ' # set field separator to ","
FNR==NR { # run this only on first file "changed.txt"
a[$1]=$0 # store "changed.txt" in array "a" using first field as index
next # skip to next record
}
{ # run this when second file is read "total.txt"
print (a[$1]?a[$1]:$0) # if filed exist in "changed" use that, else use "total"
}' changed.txt total.txt # read these files
答案 1 :(得分:2)
这可能适合你(GNU sed):
sed -r 's/^([^,]*,).*/s|^\1.*|&|/' changed.txt | sed -f - total.txt > output.txt
答案 2 :(得分:1)
这个单行为你工作吗?
awk -F, 'NR==FNR{a[$1]=$0;next}a[$1]{$0=a[$1]}7' change total
试验:
kent$ head c t
==> c <==
order3,8,item3
order8,12,item8
==> t <==
order1,5,item1
order2,6,item2
order3,7,item3
order4,6,item4
order8,9,item8
kent$ awk -F, 'NR==FNR{a[$1]=$0;next}a[$1]{$0=a[$1]}7' c t
order1,5,item1
order2,6,item2
order3,8,item3
order4,6,item4
order8,12,item8
答案 3 :(得分:0)
读取第一个changed.txt
文件并将数据保存在哈希中。然后比较是否存在total.txt
的第一个字段并替换值:
awk '
BEGIN { FS = OFS = "," }
NR == FNR { a[$1] = $2; next }
{ if ( $1 in a ) { $2 = a[$1] } print }
' changed.txt total.txt
它产生:
order1,5,item1
order2,6,item2
order3,8,item3
order4,6,item4
order8,12,item8