Awk print from nth column to last

awktext processing

I want to print all columns from nth to last column of a line

Input String in file

vddp vddpi vss cb0 cb1 cb2 cb3 ct0 ct1 ct2 ct3  

Command

cat <file> | awk ' { for (i=3; i<=NF; i++)   print $i }'

Current Output

cb0
cb1
cb2
cb3
ct0
ct1
ct2
ct3

Desired Output

cb0 cb1 cb2 cb3 ct0 ct1 ct2 ct3

I am trying the awk iteration, but cannot get desired output

Best Answer

awk -v n=4 '{ for (i=n; i<=NF; i++) printf "%s%s", $i, (i<NF ? OFS : ORS)}' input

This will take n as the value of n and loop through that number through the last field NF, for each iteration it will print the current value, if that is not the last value in the line it will print OFS after it (space), if it is the last value on the line it will print ORS after it (newline).

$ echo 'vddp vddpi vss cb0 cb1 cb2 cb3 ct0 ct1 ct2 ct3' |
> awk -v n=4 '{ for (i=n; i<=NF; i++) printf "%s%s", $i, (i<NF ? OFS : ORS)}'
cb0 cb1 cb2 cb3 ct0 ct1 ct2 ct3
Related Question