AWK|BASH, use double FS and ternary operator -
is possible?
i wondering how do:
- count fields differentiated comma.
- only obtained first field of previous step, count words differentiated space.
- if there more 2 words, print
nf
, otherwise$0
.
input
cellular biol immunogenet, rosario escuela estadist, medellin medellin
expected output
rosario escuela estadist, medellin medellin
you can use awk command
awk -f, '{if (split($1, a, " ") > 2) print $nf; else print}' file
this output
rosario escuela estadist, medellin medellin
if want rid of space before $nf
, use awk command
awk -f, '{if (split($1, a, " ") > 2) print substr($nf, 2); else print}' file
this output
rosario escuela estadist, medellin medellin
explanation
we use -f,
set field separator comma.
for every line, split first field array a
, using single space separator. check if return value greater 2 (the split function returns number of elements), , print substr($nf, 2)
, removes space @ beginning of $nf
. otherwise, print whole line using print
.
Comments
Post a Comment