4reel77
4reel77

Reputation: 401

awk one liner to create third column field from two columns

Looking for a awk one liner create a new third field based on the second field.

starting data:

column1,test.columna
column2,test1.columnb
column3,test2.columnc

end data where test.columna (2nd field) is now "columnatest" (for the first row) in a new third field separated by ",".

column1,test.columna,columnatest
column2,test1.columnb,columnbtest1
column3,test2.columnc,columnctest2

getting the third column by itself easy enough :

awk -F, '{print $2}' a | awk -F. '{print $2$1}'

columnatest
columnbtest1
columnctest2

Having trouble combining all three fields.

Upvotes: 1

Views: 167

Answers (2)

RARE Kpop Manifesto
RARE Kpop Manifesto

Reputation: 2801

 echo '
column1,test.columna
column2,test1.columnb
column3,test2.columnc' | 

mawk -F, 'sub("$",FS substr($2 $2, 1 + index($2, "."), length($2)-1))'

column1,test.columna,columnatest
column2,test1.columnb,columnbtest1
column3,test2.columnc,columnctest2

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203522

$ awk 'BEGIN{FS=OFS=","} {split($2,f,/\./); print $0, f[2] f[1]}' file
column1,test.columna,columnatest
column2,test1.columnb,columnbtest1
column3,test2.columnc,columnctest2

Upvotes: 2

Related Questions