Reputation: 79
I want to move the data in the last column into a new column with a particular column name in the file if possible. The last column from which we move should be empty.
Input:Sample file
Col1,col2,col3,col4
1,abc,test,"Codes:1,2,3,4"
2,xyz,test1,"Codes:12,22,3,4"
3,def,test2,"Codes:1,22,3,41"
Output:
Col1,col2,col3,col4,codes
1,abc,test,,"1,2,3,4"
2,xyz,test1,,"12,22,3,4"
3,def,test2,,"1,22,3,41"
I was able to cut the data,but unable to move it into a new column
cut -d',' -f12- < file.csv | awk -F":" '{print $2}' | awk -F"\"" '{print $1}'
I'm not sure if this is the right approach too.
Upvotes: 0
Views: 314
Reputation: 133428
Could you please try following.
awk '{sub(/\,\"[a-zA-Z]+:/,",,\"")} 1' Input_file
Upvotes: 0
Reputation: 5006
Using sed :
sed '1s/$/,codes/;s/"Codes:/,"/' <file>
Using awk :
awk 'BEGIN{FS=OFS=","}NR==1{print $0, "codes"}NR!=1{gsub("Codes:",""); $4 = FS $4; print}' <file>
Upvotes: 1
Reputation: 58351
This might work for for you (GNU sed):
sed '1s/$/,codes/;1!s/"Codes:/,"/' file
For the first lines append ,codes
, all other lines replace "Codes:
by ,"
.
Upvotes: 0
Reputation: 203169
$ awk 'NR==1{print $0",codes"; next} {sub(/"[^:]+:/,",\"")} 1' file
Col1,col2,col3,col4,codes
1,abc,test,,"1,2,3,4"
2,xyz,test1,,"12,22,3,4"
3,def,test2,,"1,22,3,41"
Upvotes: 1