Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use awk command to cut field?

Tags:

shell

there is a file ,which you can get by command ps aux in linux.

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   2280   732 ?        Ss   08:20   0:00 init [2]  
root       327  0.0  0.1   2916  1456 ?        Ss   08:20   0:00 udevd --daemon
root      1681  0.0  0.0   2376   800 ?        Ss   08:20   0:00 /sbin/rpcbind -w
root      2071  0.0  0.1  27920  1708 ?        Sl   08:20   0:00 /usr/sbin/rsyslogd -c5

i want to get the content of last field,such as:

COMMAND
init [2]
udevd --daemon
/sbin/rpcbind -w
/usr/sbin/rsyslogd -c5

when i use , $ awk '{print $11}' test,i get:

COMMAND
init
udevd
/sbin/rpcbind
/usr/sbin/rsyslogd

when i use ,$ awk '{print $12}' test,i get :

[2]
--daemon
-w
-c5

how can i do?

like image 525
showkey Avatar asked Aug 31 '25 10:08

showkey


2 Answers

awk '{for(i=1;i<11;i++)$i="";print }'

or you can just do:

awk '{print $11,$12}'
like image 112
Vijay Avatar answered Sep 03 '25 04:09

Vijay


You can loop over the words of the last field and print them:

ps aux | awk '{ for( i=11 ; i <=NF ; i++ ) { printf( "%s ", $i ) } ; print "" }'

If you want to process the command afterwards, you can put it in a variable:

ps aux | awk '{ CMD = "" ; for( i=11 ; i <=NF ; i++ ) { CMD = CMD " " $i } ; sub( /^ /, "", CMD ) ; print CMD }'