Reputation: 2559
Is it possible?
I was wondering how to do:
NF
, otherwise $0
.Input
cellular biol immunogenet, rosario
escuela estadist, medellin
medellin
Expected output
rosario
escuela estadist, medellin
medellin
Upvotes: 1
Views: 119
Reputation: 1256
You can use this awk command
awk -F, '{if (split($1, a, " ") > 2) print $NF; else print}' file
This will output
rosario
escuela estadist, medellin
medellin
If you want to get rid of the space before $NF
, use this awk command
awk -F, '{if (split($1, a, " ") > 2) print substr($NF, 2); else print}' file
This will output
rosario
escuela estadist, medellin
medellin
Explanation
We use the -F,
to set the field separator to a comma.
For every line, we split the first field into the array a
, using a single space as the separator. We check if the return value is greater than 2 (the split function returns the number of elements), and then we print the substr($NF, 2)
, which removes the space at the beginning of $NF
. Otherwise, we just print the whole line using print
.
Upvotes: 2
Reputation: 6181
awk -F, 'split($1,a,/ /) > 2 {$0=$NF} 1' input.txt
split($1,a,/ /)
splits $1
on the regex / /
and stores them into the array a
. It returns the resulting length of the array.
So if $1
is split into more than two words, then change the line $0
to $NF
.
The lone 1
is a shorthand that makes sure it prints each line.
Upvotes: 1