Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print all the columns after a particular number using awk?

Tags:

shell

awk

On shell, I pipe to awk when I need a particular column.

This prints column 9, for example:

... | awk '{print $9}' 

How can I tell awk to print all the columns including and after column 9, not just column 9?

like image 686
Lazer Avatar asked Feb 22 '11 17:02

Lazer


People also ask

What is awk '{ print $1 }'?

If you notice awk 'print $1' prints first word of each line. If you use $3, it will print 3rd word of each line.

What is awk '{ print $2 }'?

awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output. xargs kill -${2:-'TERM'} takes the process IDs from the selected sidekiq processes and feeds them as arguments to a kill command.


2 Answers

awk '{ s = ""; for (i = 9; i <= NF; i++) s = s $i " "; print s }' 
like image 74
Amadan Avatar answered Sep 28 '22 10:09

Amadan


When you want to do a range of fields, awk doesn't really have a straight forward way to do this. I would recommend cut instead:

cut -d' ' -f 9- ./infile 

Edit

Added space field delimiter due to default being a tab. Thanks to Glenn for pointing this out

like image 39
SiegeX Avatar answered Sep 28 '22 12:09

SiegeX