I am trying to append multiple csv files in to one. How can I enhance the below script so that an additional column is added. Let's call it "tag". The values in the tag should be the filename from which the record has been appended.
flag=0
for f in $@/*.csv;
do
k=$(wc -l<"$f" )
if [ $flag -eq 0 ];
then
head -n $k "$f" > out.csv
flag=1
else
tail -n +2 "$f" >> out.csv
fi
done
Using @Shawn's approach below I am getting this:-
$ cat TEST1/a.csv
h1,h2,h3
a,b,c
d,e,f
$ cat TEST1/b.csv
h1,h2,h3
1,2,3
4,5,6
$ awk 'NR == 1 { print $0 ",tag"; next }
FNR == 1 { next }
{ print $0 "," FILENAME }' TEST1/a.csv TEST1/b.csv
,tag2,h3
,TEST1/a.csv
,TEST1/a.csv
,TEST1/b.csv
,TEST1/b.csv