How to add a new column to a CSV file

How to add a new column to a CSV file with the column name as the date in the data fill using a text file in a shell script.

Input file: input.csv

DESC,20-07,21-07,22-07,23-07,24-07,25-07 A,1,2,3,4,5 B,WD,DSF,GFH,SDF,SDF C,FG,ZX,CRST,SDF,SEF 

text file: abc.txt

 26-07 ew we we 

I need an output file: output.csv

 DESC,20-07,21-07,22-07,23-07,24-07,25-07,26-07 A,1,2,3,4,5,ew B,WD,DSF,GFH,SDF,SDF,we C,FG,ZX,CRST,SDF,SEF,we 

I want a script that adds a column to the CSV file daily.

+4
source share
2 answers

You can do this with the paste command:

 $ paste -d',' input abc DESC,20-07,21-07,22-07,23-07,24-07,25-07,26-07 A,1,2,3,4,5,ew B,WD,DSF,GFH,SDF,SDF,we C,FG,ZX,CRST,SDF,SEF,we 

Or the pr command:

 $ pr -mts',' input abc DESC,20-07,21-07,22-07,23-07,24-07,25-07,26-07 A,1,2,3,4,5,ew B,WD,DSF,GFH,SDF,SDF,we C,FG,ZX,CRST,SDF,SEF,we 
+4
source

Another option might be awk:
awk 'NR == FNR {if(x == 0) {x=1};a[x]=$0;x++;next}{for(z=1; z < x; z++){if (z <= NF){print a[z]","$z} else {print a[z]}}}' input.csv abc.txt > output.csv

This considers the case where input.csv has more rows than abc.txt has columns.

0
source

Source: https://habr.com/ru/post/1493493/


All Articles