sjsam
sjsam

Reputation: 21955

Shorter awk to print starting from Nth field

To print fields starting from N, say N=5

awk '{for(i=5;i<=NF;i++){if(i<NF){printf "%s%s",$i,OFS}else{print $i}}}' 
# This is a bit lengthy!

Any shorter awk available?

Upvotes: 1

Views: 90

Answers (2)

Lee Netherton
Lee Netherton

Reputation: 22482

For very simple applications, cut may be better suited:

$ echo "1 2 3 4 5 6 7 8" | cut -d' ' -f5-
5 6 7 8

Upvotes: 1

Tom Fenech
Tom Fenech

Reputation: 74596

With the default field separator and GNU awk (version 4+), this is marginally shorter:

gawk '{ sub(/^\s*(\S+\s+){4}/, "") }1' file

This removes 4 fields from the start of the line, including any leading whitespace.

Upvotes: 1

Related Questions