Reputation: 51
I am trying to add a header to a split file but with this code the header is appearing every other line:
awk -F, '{print "eid,devicetype,meterid,lat,lng" > $7"-"$6".csv"}{print $1",", $2",", $3",", $4",", $5"," >> $7"-"$6".csv"}' path/filename
The awk code by itself works but I need to apply a header in the file. The script splits the file based on the values in columns 6 & 7 as well as names the end file with those values. Then it removes columns 6 & 7 it only puts columns 1 - 5 in the output file. This is on Unix in a shell script run from PowerCenter. I am sure it is probably simple fix for others more experienced.
Upvotes: 0
Views: 180
Reputation: 785128
You can use:
awk -F, '!a[$7,$6]++{print "eid,devicetype,meterid,lat,lng" > $7 "-" $6 ".csv"}
{print $1,$2,$3,$4,$5 > $7 "-" $6 ".csv"}' OFS=, /path/filename.csv
NR==1
will make sure that header is printed for 1st record.
Upvotes: 1
Reputation: 203463
awk '
BEGIN { FS=OFS="," }
{ fname = $7 "-" $6 ".csv" }
!seen[fname]++ { print "eid", "devicetype", "meterid", "lat, "lng" > fname}
{ print $1, $2, $3, $4, $5 > fname }
' path/filename
Upvotes: 1