Try this
$ awk -F, 'NR<2{split(gensub(/Citty/,"City","g",$0),a,FS)}NR==2{for(b=2;b<=NF;b+=2){c=c a[b]" "$b","}print gensub(/,$/,"",1,c)}NR>2{print gensub(/(^,|" *",)/,"","g",$0)}' inp
Product Name,City Location,Price Per Unit
banana,CA,5.7
apple,FL,2.3
$
Same code is more readable if split across a few lines :
$ awk -F, '
> NR<2{split(gensub(/Citty/,"City","g",$0),a,FS)}
> NR==2{for(b=2;b<=NF;b+=2){c=c a[b]" "$b","}print gensub(/,$/,"",1,c)}
> NR>2{print gensub(/(^,|" *",)/,"","g",$0)}' inp
Product Name,City Location,Price Per Unit
banana,CA,5.7
apple,FL,2.3
$
If 1st line, split the line into array elements within a. Fix the Citty->City typo.
If 2nd line, starting with the 2nd column, print the corresponding column from 1st line together with this column. Repeat for each column, going in 2 column increments. Strip the trailing ,
.
After 2nd line, replace any leading ,
or any "<spaces>",
with an empty string and then print the result.
Tested ok on GNU Awk 4.0.2
Try it online!
Best Answer
That's what
awk
is for:Or, if you can have spaces inside your fields, specify tab as the field separator:
Alternatively, you could use Perl:
To do this for multiple files, you can use a shell loop (assuming you want to run this on all files in the current directory):