linux - AWK, add text before each column strange behaviour -


i need add before each column different text , i'm using command:

top -b -n1  | tail -n +8 | grep -v top | head -6 | awk 'begin { ofs = "\t" } $1 = "pid=" $1, $2 = "user=" $2' | awk '{ print strftime("%y-%m-%d %h:%m:%s"), $0; fflush(); }' 

this works, if add column:

top -b -n1  | tail -n +8 | grep -v top | head -6 | awk 'begin { ofs = "\t" } $1 = "pid=" $1, $2 = "user=" $2, $9 = "cpu=" $9' | awk '{ print strftime("%y-%m-%d %h:%m:%s"), $0; fflush(); }' 

i error:

awk: linea com.:1: begin { ofs = "\t" } $1 = "pid=" $1, $2 = "user=" $2, $9 = "%cpu=" $9 awk: linea com.:1:                                 ^ syntax error 

the syntax error on "," after "$2".

i don't understand why. full output of command (without prepend text before each column) example:

2015-05-31 11:16:08 1589 root 12,4 2,2 xorg  2015-05-31 11:16:08 3 root 6,2 0,0 ksoftirqd/0  2015-05-31 11:16:08 1 root 0,0 0,1 init  2015-05-31 11:16:08 2 root 0,0 0,0 kthreadd  2015-05-31 11:16:08 5 root 0,0 0,0 kworker/0:0h  2015-05-31 11:16:08 7 root 0,0 0,0 rcu_sched 

and need result this:

2015-05-31 11:16:08 1589 root 12,4 2,2 xorg  2015-05-31 11:16:08 pid=3 user=root %cpu=6,2 %mem=0,0 command=ksoftirqd/0  2015-05-31 11:16:08 pid=1 user=root %cpu=0,0 %mem=0,1 command=init  2015-05-31 11:16:08 pid=2 user=root %cpu=0,0 %mem=0,0 command=kthreadd  2015-05-31 11:16:08 pid=5 user=root %cpu=0,0 %mem=0,0 command=kworker/0:0h  2015-05-31 11:16:08 pid=7 user=root %cpu=0,0 %mem=0,0 command=rcu_sched 

until column user works. can me please? much

the syntax error on "," after "$2". don't understand why.

because multiple commands appear on same line need separated ; , not ,. (looking @ manual help)

however, using lot of tools tasks can done awk. can use single awk command that:

top -b -n1 |\   awk 'nr>7&&!/top/&&c++<6{$1= "pid=" $1;$2= "user=" $2;print strftime("%y-%m-%d %h:%m:%s"), $0}' ofs='\t' 

explained better muli-line script:

    nr>7           # skip first 7 lines      &&  !/top/         # skip lines containing term 'top' &&  c++<6   {      # limit output 6 lines      $1= "pid=" $1  # fix pid field     $2= "user=" $2 # fix user fields      # print modified line timestamp in front of     print strftime("%y-%m-%d %h:%m:%s"), $0 } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -