Hi,
So I was trying this awk snippet
awk -F, '$1 ~ /Match/' File
This is my sample file output
Name,Age,Nationality,Description
Jack,20,American,Tall, caucasian, lean
Mary,30,British,Short,white,slim
I would expect expected Output to be, when a certain match is met say $1 is equal to Jack
Name Age Nationality Description
Jack 20 American Tall, caucasian, lean
But as you can tell, because of delimiter "," it delimits the description output as below
Name Age Nationality Description
Jack 20 American Tall caucasian lean
Throwing results out of columns boundary.
Any idea how to have a workaround of this?
mjf
2
Here is one solution:
awk -F, '{if (NR==1) {gsub(","," ",$0); print $0} else {if ($1 ~ /Jack/) {print $1,$2,substr($0,length($1)+length($2)+3,length($0))}}}' File
Name Age Nationality Description
Jack 20 American,Tall, caucasian, lean
Aia
3
If you want to display the description without fixing commas inconsistency among records:
awk -F, '$1 ~ /Jack|Mary/ {match($0, $4); print $1,$2,$3, substr($0, RSTART)}' file
Jack 20 American Tall, caucasian, lean
Mary 30 British Short,white,slim
If you want to fix the commas inconsistency of descriptions
awk -F, '$1 ~ /Jack|Mary/ {match($0, $4); d = substr($0, RSTART); gsub(/(,|, )/, ", ", d); print $1,$2,$3, d}' file
Jack 20 American Tall, caucasian, lean
Mary 30 British Short, white, slim