Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Can't figure out what field separator to use in awk.... Post 302324080 by Gee-Money on Wednesday 10th of June 2009 12:15:55 AM
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.
 

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
Locale::Codes::Script(3)				User Contributed Perl Documentation				  Locale::Codes::Script(3)

NAME
Locale::Codes::Script - standard codes for script identification SYNOPSIS
use Locale::Codes::Script; $script = code2script('phnx'); # 'Phoenician' $code = script2code('Phoenician'); # 'Phnx' $code = script2code('Phoenician', LOCALE_CODE_NUMERIC); # 115 @codes = all_script_codes(); @scripts = all_script_names(); DESCRIPTION
The "Locale::Codes::Script" module provides access to standards codes used for identifying scripts, such as those defined in ISO 15924. Most of the routines take an optional additional argument which specifies the code set to use. If not specified, the default ISO 15924 four-letter codes will be used. SUPPORTED CODE SETS
There are several different code sets you can use for identifying scripts. A code set may be specified using either a name, or a constant that is automatically exported by this module. For example, the two are equivalent: $script = code2script('phnx','alpha'); $script = code2script('phnx',LOCALE_SCRIPT_ALPHA); The codesets currently supported are: alpha, LOCALE_SCRIPT_ALPHA This is a set of four-letter (capitalized) codes from ISO 15924 such as 'Phnx' for Phoenician. It also includes additions to this set included in the IANA language registry. The Zxxx, Zyyy, and Zzzz codes are not used. This is the default code set. num, LOCALE_SCRIPT_NUMERIC This is a set of three-digit numeric codes from ISO 15924 such as 115 for Phoenician. ROUTINES
code2script ( CODE [,CODESET] ) script2code ( NAME [,CODESET] ) script_code2code ( CODE ,CODESET ,CODESET2 ) all_script_codes ( [CODESET] ) all_script_names ( [CODESET] ) Locale::Codes::Script::rename_script ( CODE ,NEW_NAME [,CODESET] ) Locale::Codes::Script::add_script ( CODE ,NAME [,CODESET] ) Locale::Codes::Script::delete_script ( CODE [,CODESET] ) Locale::Codes::Script::add_script_alias ( NAME ,NEW_NAME ) Locale::Codes::Script::delete_script_alias ( NAME ) Locale::Codes::Script::rename_script_code ( CODE ,NEW_CODE [,CODESET] ) Locale::Codes::Script::add_script_code_alias ( CODE ,NEW_CODE [,CODESET] ) Locale::Codes::Script::delete_script_code_alias ( CODE [,CODESET] ) These routines are all documented in the Locale::Codes::API man page. SEE ALSO
Locale::Codes The Locale-Codes distribution. Locale::Codes::API The list of functions supported by this module. http://www.unicode.org/iso15924/ Home page for ISO 15924. http://www.iana.org/assignments/language-subtag-registry The IANA language subtag registry. AUTHOR
See Locale::Codes for full author history. Currently maintained by Sullivan Beck (sbeck@cpan.org). COPYRIGHT
Copyright (c) 1997-2001 Canon Research Centre Europe (CRE). Copyright (c) 2001-2010 Neil Bowers Copyright (c) 2010-2013 Sullivan Beck This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.16.3 2013-02-27 Locale::Codes::Script(3)
All times are GMT -4. The time now is 07:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy