Can't figure out what field separator to use in awk....


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Can't figure out what field separator to use in awk....
# 1  
Old 06-09-2009
Can't figure out what field separator to use in awk....

Hi Friends,

Scripting newb here. So I'm trying to create a geektool script that uses awk and printf to output certain fields from top (namely command, cpu%, rsize, pid and time, in that order).

Here's the input from the top process that I'm putting into awk:

Code:
  PID COMMAND      %CPU   TIME   #TH #PRTS #MREGS RPRVT  RSHRD  RSIZE  VSIZE
66110 GeekTool    11.7%  3:16:32  73   923-     0     0-     0-   55M  1006M 
31858 firefox-bi   5.8% 52:15.57  18   227      0     0-     0-  320M  1273M 
87943 perl         3.7%  0:00.25   1    13      0     0-     0- 5368K   589M 
87902 perl         3.5%  0:00.31   1    14+     0     0-     0- 7104K   590M 
87942 perl         3.0%  0:00.24   1    13      0     0-     0- 5368K   589M 
87941 perl         2.9%  0:00.23   1    13      0     0-     0- 5368K   589M 
   63 coreservic   2.6% 15:55.41   2   243+     0     0-     0-   18M   645M 
66070 WindowServ   2.0% 27:07.05   6   387+     0     0-     0-   84M  1015M 
    0 kernel_tas   1.6% 24:31.65  62     2      0     0-     0   157M  1122M 
   15 distnoted    1.2%  8:31.58   1    66+     0     0-     0-  852K   586M 
66106 Last.fm      0.8%  7:25.17  10   167      0     0-     0-   27M   993M 
87946 top          0.7%  0:00.14   1    32+     0     0-     0- 1016K   586M 
87551 Terminal     0.7%  0:00.42   5   107      0     0-     0- 9532K   927M 
87930 top          0.5%  0:00.19   1    17+     0     0-     0- 1020K   587M 
   11 DirectoryS   0.5%  3:38.61   5    84      0     0-     0- 3380K   589M 
66163 System Eve   0.4%  2:18.47   2    86+     0     0-     0- 6452K   901M

As you can see, the last process listed is two words (System Eve), therefore with the default field separator awk sees System as field $2 and Eve as field $3, and thus outputs them to different columns, much to my dismay. For the life of me I can't figure out what the appropriate field separator should be given the way the input is broken up; everything I have tried has failed miserably.

Here's my script so far:
Code:
top -FR -l2 -ocpu | grep -v ' 0.0% ..:' | awk '{printf( "%-12s %-5s %-5s %-5s %s\n", $2,$3,$10,$1,$4 ) }' | sed -n '15,$p'

And here's an example of the current (wrong) output:

Code:
COMMAND      %CPU  RSIZE PID   TIME
GoogleDesk   0.5%  59M   57    10:49.81
System       Eve   0-    66163 0.4%
Terminal     0.4%  9428K 8158  0:00.34

Any help would be greatly appreciated.

Thanks,

Thom
# 2  
Old 06-10-2009
Whipped this up...fun stuff, i'll leave the output formatting up to you

i created a file with your top input:
Code:
$ cat file
  PID COMMAND      %CPU   TIME   #TH #PRTS #MREGS RPRVT  RSHRD  RSIZE  VSIZE
66110 GeekTool    11.7%  3:16:32  73   923-     0     0-     0-   55M  1006M
31858 firefox-bi   5.8% 52:15.57  18   227      0     0-     0-  320M  1273M
87943 perl         3.7%  0:00.25   1    13      0     0-     0- 5368K   589M
87902 perl         3.5%  0:00.31   1    14+     0     0-     0- 7104K   590M
87942 perl         3.0%  0:00.24   1    13      0     0-     0- 5368K   589M
87941 perl         2.9%  0:00.23   1    13      0     0-     0- 5368K   589M
   63 coreservic   2.6% 15:55.41   2   243+     0     0-     0-   18M   645M
66070 WindowServ   2.0% 27:07.05   6   387+     0     0-     0-   84M  1015M
    0 kernel_tas   1.6% 24:31.65  62     2      0     0-     0   157M  1122M
   15 distnoted    1.2%  8:31.58   1    66+     0     0-     0-  852K   586M
66106 Last.fm      0.8%  7:25.17  10   167      0     0-     0-   27M   993M
87946 top          0.7%  0:00.14   1    32+     0     0-     0- 1016K   586M
87551 Terminal     0.7%  0:00.42   5   107      0     0-     0- 9532K   927M
87930 top          0.5%  0:00.19   1    17+     0     0-     0- 1020K   587M
   11 DirectoryS   0.5%  3:38.61   5    84      0     0-     0- 3380K   589M
66163 System Eve   0.4%  2:18.47   2    86+     0     0-     0- 6452K   901M


created this script, and named it "stuff":

Code:
awk ' BEGIN {print "Command %CPU RSIZE PID Time"}

NR > 1 {        for ( i = 1; i <= NF; i++ ) {
                        if (i == 1) {
                                pid = $i
                                commandname = ""
                                continue
                        }

                        if (! ($i ~ /[0-9][0-9]*\.[0-9]%/)) {
                                commandname = sprintf("%s %s", commandname, $i)
                        } else {
                                cpu = $i
                                time = $(i+1)
                                rsize = $(NF-1)
                                break
                        }

                }
                printf("%s %s %s %s %s\n",commandname,cpu,rsize,pid,time)
}'

voila:

Code:
$ cat file | ./stuff
Command %CPU RSIZE PID Time
 GeekTool 11.7% 55M 66110 3:16:32
 firefox-bi 5.8% 320M 31858 52:15.57
 perl 3.7% 5368K 87943 0:00.25
 perl 3.5% 7104K 87902 0:00.31
 perl 3.0% 5368K 87942 0:00.24
 perl 2.9% 5368K 87941 0:00.23
 coreservic 2.6% 18M 63 15:55.41
 WindowServ 2.0% 84M 66070 27:07.05
 kernel_tas 1.6% 157M 0 24:31.65
 distnoted 1.2% 852K 15 8:31.58
 Last.fm 0.8% 27M 66106 7:25.17
 top 0.7% 1016K 87946 0:00.14
 Terminal 0.7% 9532K 87551 0:00.42
 top 0.5% 1020K 87930 0:00.19
 DirectoryS 0.5% 3380K 11 3:38.61
 System Eve 0.4% 6452K 66163 2:18.47

Hope this helps, and hope you can learn something about awk from this.
# 3  
Old 06-10-2009
Try...
Code:
top -FR -l2 -ocpu | awk '!/ 0.0% ..:/&&(++c>=15||NR==1){printf( "%-12s %-5s %-5s %-5s %s\n", substr($0,7,10),$(NF-8),$(NF-1),$1,$(NF-7) ) }'

# 4  
Old 06-10-2009
Thanks gents! Both worked perfectly.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk field separator not working

Hi, can some some help to get me the right results, I have few text files, need to grep few columns from each file and get the results in one row with comma separated. my code is #folder=/nz/kit/log/backupsvr folder=/export/home/nz/valai/tmpfiles/ echo $folder for entry in `ls... (4 Replies)
Discussion started by: ValaiG
4 Replies

2. Shell Programming and Scripting

Field Separator in printf (awk)

I can not figure out how to set the Output filed separator in awk when using printf. Example: cat file some data here_is_more information Requested output some------------data her_is_more-----information Here are some that does not work: awk '{printf "%-15s %s\n",$1,$2}' OFS="-" file... (9 Replies)
Discussion started by: Jotne
9 Replies

3. Shell Programming and Scripting

awk field separator help -

Hi Experts , file : - How to construct the awk filed separator so that $1, $2 $3 , can be assigned to the each "" range. I am trying : awk -F"]" '{print $1}' but it is printing the entire file. Not first field. The desired output needed for first field... (9 Replies)
Discussion started by: rveri
9 Replies

4. Shell Programming and Scripting

awk field separator

I need to set awk field separator to ";", but I need to avoid ";EXT". so that echo a;b;c;EXTd;e;f | awk -F";" '{print $3}' would give "c;EXTd" (2 Replies)
Discussion started by: locoroco
2 Replies

5. UNIX for Dummies Questions & Answers

awk - output field separator

In awk, how do I print all fields with a specified output field separator? I have tried the following, which does not print the output FS: echo a b c d | awk 'BEGIN{OFS = ";"}{print $0}' (3 Replies)
Discussion started by: locoroco
3 Replies

6. Shell Programming and Scripting

awk - show field separator

I am using this code to insert something into a csv file: awk -F";" -v url=$url -v nr=$nr 'NR==nr{$2=url$2}1' file Why do I get the output field1 field2 instead of field1;field2 I have given -F";", so the field separator should surely be ";". (1 Reply)
Discussion started by: locoroco
1 Replies

7. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

8. Shell Programming and Scripting

awk (nawk) field separator

Hi; i have a file and i want to get; - If the last word in line 14 is NOT equal to "Set."; then print 2nd, 3rd, 4th and 5th values of 3rd line. and my code is: nawk 'NR==14 {if ($NF!="Set.") (NR==3{print $2,$3,$4,$5}) }' file.txt but no result?? :confused::(:confused::( (4 Replies)
Discussion started by: gc_sw
4 Replies

9. Shell Programming and Scripting

Field separator in awk

Hi I need to check if field separator I am using in awk statement is " : ", for example: TIME=12:59 HOUR=`echo "$TIME" | awk '{FS=":"; print $1}'` MINUTES=`echo "$TIME" | awk '{FS=":"; print $2}'` Is there a way to check within the above awk statement ? Thanks for help -A (2 Replies)
Discussion started by: aoussenko
2 Replies

10. Shell Programming and Scripting

dynamically change awk Field Separator FS

Hi All, I was wondering if anyone knew how to dynamically change the FS in awk to accept vairiable containing a field separator. the current code is as below and does not work when i introduce the dynamic FS change :-( validate_source_file() { source_file=$1 ... (2 Replies)
Discussion started by: satnamx
2 Replies
Login or Register to Ask a Question