How to print all fields after a certain field with awk on Linux?

How to print all fields after a certain field with awk on Linux?

Say, I want to print out all fields after field $3:

a b c d e f
a b b b
a a c d

should be transformed to

d e f
b
d

You may use a for loop in awk to print fields from $4 to $NF with a space as the separator:

awk '{for (i=4; i<NF; i++) printf $i " "; if (NF >= 4) print $NF; }' in.txt

printf print the field and a following space. The if statement makes sure nothing is printed out if the number of fields is less than 4. The last print statement prints the last field without the additional space yet with a newline.

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *