Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Can't figure out what field separator to use in awk.... Post 302324066 by thom.mattson on Tuesday 9th of June 2009 08:34:45 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
JOIN(1) 						      General Commands Manual							   JOIN(1)

NAME
join - relational database operator SYNOPSIS
join [ options ] file1 file2 DESCRIPTION
Join forms, on the standard output, a join of the two relations specified by the lines of file1 and file2. If file1 is `-', the standard input is used. File1 and file2 must be sorted in increasing ASCII collating sequence on the fields on which they are to be joined, normally the first in each line. There is one line in the output for each pair of lines in file1 and file2 that have identical join fields. The output line normally con- sists of the common field, then the rest of the line from file1, then the rest of the line from file2. Fields are normally separated by blank, tab or newline. In this case, multiple separators count as one, and leading separators are dis- carded. These options are recognized: -an In addition to the normal output, produce a line for each unpairable line in file n, where n is 1 or 2. -e s Replace empty output fields by string s. -jn m Join on the mth field of file n. If n is missing, use the mth field in each file. -o list Each output line comprises the fields specifed in list, each element of which has the form n.m, where n is a file number and m is a field number. -tc Use character c as a separator (tab character). Every appearance of c in a line is significant. SEE ALSO
sort(1), comm(1), awk(1) BUGS
With default field separation, the collating sequence is that of sort -b; with -t, the sequence is that of a plain sort. The conventions of join, sort, comm, uniq, look and awk(1) are wildly incongruous. JOIN(1)
All times are GMT -4. The time now is 10:55 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy