using awk to selectively change word order in a column -


i want change text:

fred flintstone:father:male wilma flintstone:mother:female flintstone, pebbles:daughter:female barney rubble:father:male rubble, betty:mother:female bam bam rubble:son:male 

into:

flintstone, fred:father:male flintstone, wilma:mother:female flintstone, pebbles:daughter:female rubble, barney:father:male rubble, betty:mother:female rubble, bam bam:son:male 

is possible in 1 line of awk? or better extract first column manipulate text , reimport column?

here gawk 1 liner:

awk -f: '$1=gensub(/([^,]+)[ ]+([^ ,]+)/,"\\2, \\1","1",$1)' yourfile 
  • it uses : delimiter -f option
  • the gensub function used on $1 in pattern part:
    • if replacement possible, expression true , modifies $1
      • the default action print used implicitly.
    • if expression not match (due comma in $1) line implicitly printed

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -