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
asked Aug 3 by anonymous
edited Aug 6 by SA

1 Answer

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.

answered Aug 6 by Eric Z Ma (43,790 points)

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...