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.

Similar Posts

Leave a Reply

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