我有这种文本文件:id; firstname; client; invoice; comments(这是一个例子)
001;peter;35621;C132;foo
002;jane;12345;A546;
003;robert;78945;Z456;some comments
001;peter;35621;Y456;blabla
004;terry;89740;T897;
003;robert;78945;L546;bar
我想要做的是根据客户端在每行末尾的另一个字段中连接发票字段。我只能使用bash或unix cli工具(awk,sed,cut等...) 在这种情况下应该产生:
001;peter;35621;C132;foo;;C132/Y456
002;jane;12345;A546;;A546
003;robert;78945;Z456;some comments;Z456/L546
001;peter;35621;Y456;blabla;C132/Y456
004;terry;89740;T897;;T897
003;robert;78945;L546;bar;Z456/L546
到目前为止我做了什么:(但我对结果并不满意,因为它看起来真的很难看)
#!/bin/bash
file="myfile.txt"
while IFS=\; read id firstname client invoice comments
do
catInvoice=""
while IFS=\; read xid xfirstname xclient xinvoice xcomments
do
if [ $client == $xclient ]
then
catInvoice="$catInvoice/$xinvoice"
fi
done < $file
catInvoice=`echo $catInvoice | sed 's/^\///'`
echo "$id;$firstname;$client;$invoice;$comments;$catInvoice"
done < $file
有什么建议吗? 感谢。
答案 0 :(得分:2)
两次传递到文件,但是诀窍:
awk '
BEGIN{FS=OFS=";"}
NR==FNR{a[$2]=(a[$2])?a[$2]"/"$4:$4;next}
{print $0,a[$2]}' textfile textfile
$ cat textfile
001;peter;35621;C132;foo
002;jane;12345;A546;
003;robert;78945;Z456;some comments
001;peter;35621;Y456;blabla
004;terry;89740;T897;
003;robert;78945;L546;bar
$ awk 'BEGIN{FS=OFS=";"}NR==FNR{a[$2]=(a[$2])?a[$2]"/"$4:$4;next}{print $0,a[$2]}' textfile textfile
001;peter;35621;C132;foo;C132/Y456
002;jane;12345;A546;;A546
003;robert;78945;Z456;some comments;Z456/L546
001;peter;35621;Y456;blabla;C132/Y456
004;terry;89740;T897;;T897
003;robert;78945;L546;bar;Z456/L546