AWK|BASH, use double FS and ternary operator -


is possible?

i wondering how do:

  1. count fields differentiated comma.
  2. only obtained first field of previous step, count words differentiated space.
  3. 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

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -