Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Using awk to print all columns from the nth to the last

This code worked until I had whitespace in the second area.

svn status | grep '\!' | gawk '{print $2;}' > removedProjs


Is there a way to have awk print everything in $2 or greater? ($3, $4.. until we don't have any more columns?)

I suppose I should add that I'm doing this in a Windows environment with Cygwin.
by

3 Answers

espadacoder11
Print all columns:

awk '{print $0}' somefile

Print all but the first column:

awk '{$1=""; print $0}' somefile

Print all but the first two columns:

awk '{$1=$2=""; print $0}' somefile
sandhya6gczb
You could use a for-loop to loop through printing fields $2 through $NF (a built-in variable that represents the number of fields on the line).

Since "print" appends a newline, you'll want to buffer the results:

awk '{out=""; for(i=2;i<=NF;i++){out=out" "$i}; print out}'

Alternatively, use printf:

awk '{for(i=2;i<=NF;i++){printf "%s ", $i}; printf "\n"}'
kshitijrana14
awk '{ for(i=3; i<=NF; ++i) printf $i""FS; print "" }'

Login / Signup to Answer the Question.