linux - Print out all remaining variables in awk -


i trying write quick awk script converts lines in format:

aaaa bbbb cccc dddd... 

to

cccc aaaa bbbb dddd... 

this script:

{ printf "%s %s %s %s", $3, $1, $2, $4 }; 

this works fine except when original input line has more 4 tokens, in case 5th , following tokens not printed.

i checked answers, using awk print columns nth last rely on setting variables "" seems cause problems if variables reused later.

is there easy way replace $4 "the substring $4 until end of line"?

in simple case need is:

$ awk '{t=$3; $3=$2; $2=$1; $1=t}1' file cccc aaaa bbbb dddd 

but in general gnu awk gensub(), \s , \s:

$ awk '{print $3, $1, $2, gensub(/^\s*(\s+\s+){3}/,"","")}' file cccc aaaa bbbb dddd 

the gensub() skips first 3 fields , leave fields , spaces between fields as-is point on:

$ cat file aaaa bbbb cccc dddd    eeee    ffff  gggg  $ awk '{print $3, $1, $2, gensub(/^\s*(\s+\s+){3}/,"","")}' file cccc aaaa bbbb dddd    eeee    ffff  gggg 

with other awks can same match()+substr():

$ awk '{match($0,/^[[:space:]]*([^[:space:]]+[[:space:]]+){3}/); print $3, $1, $2, substr($0,rlength+1)}' file cccc aaaa bbbb dddd    eeee    ffff  gggg 

or sub() , variable:

$ awk '{x=$0; sub(/^[[:space:]]*([^[:space:]]+[[:space:]]+){3}/,"",x); print $3, $1, $2, x}' file cccc aaaa bbbb dddd    eeee    ffff  gggg 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -