Problem in getting the ps information with field separator


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in getting the ps information with field separator
# 1  
Old 11-10-2008
Problem in getting the ps information with field separator

Dear all

I have the following problem that want to have your advices.
I want to capture the `ps auxwww | head -30` image with field separator (|) in my script

My expect output
USER PID %CPU %MEM SZ RSS TTY STAT STIME TIME COMMAND
root | 774 | 18.5 | 0.0 | 12 | 19716 | - | A | Nov 11 | 388530:03 | wait |
prodloe | 230044 | 3.2 | 1.0 | 86484 |86500 | - | A | 14:35:28 | 15:04 | java dom.loeReceiver -sMQSENDER -i/appl/lis/home3/prodloe/work/LOEMQSENDER.
ini |

I think I can use awk to define the input separator (" ") and output separator ("|"), but it won't work because the STIME and COMMAND may contain space in it.
I also try `ps -e -o "%u | %p | %y | %C | %a" | head -30`. However, not all the descriptors are available in my AIX version... (e.g. I cannot check STIME and %MEM using this method).

Please give me some hints. Thank you

Valentino
# 2  
Old 11-10-2008
Pipe your output to:
Code:
awk 'NR==1{OFS=" ";$1=$1;print;next}{OFS=" | ";$1=$1;print}'

# 3  
Old 11-11-2008
Quote:
Originally Posted by Franklin52
Pipe your output to:
Code:
awk 'NR==1{OFS=" ";$1=$1;print;next}{OFS=" | ";$1=$1;print}'

Thanks for reply. However, it does not work `ps auxwww | awk 'NR==1{OFS=" ";$1=$1;print;next}{OFS=" | ";$1=$1;print}'`

Furthermore, could you explain the code meaning? I don't really understand. As I know OFS stand for Output Field Separator, and what is the meaning of $1=$1 here?

Thank you for your kind assistance.

Valentino
# 4  
Old 11-11-2008
It works fine for me. If it don't give you the desired output, post the output of your ps command within code tags (to keep the spaces/indentions).
There are 2 different field separators, for the first line I used 1 space and for the other lines " | ". The command $1=$1 is to reset the field separator.

Regards
# 5  
Old 11-11-2008
Here is my `ps auxw` output
Code:
USER         PID %CPU %MEM   SZ  RSS    TTY STAT    STIME  TIME COMMAND
root       16680  5.9  0.0  384  384      - A      May 29 112986:18 wait
root         288  5.8  0.0  384  320      - A      May 29 110349:37 wait
root       57372  5.7  0.0  384  320      - A      May 29 109924:11 wait
root        8196  5.7  0.0  384  384      - A      May 29 109031:40 wait
root       61470  5.3  0.0  384  320      - A      May 29 102397:06 wait
root       12582  5.3  0.0  384  384      - A      May 29 101507:00 wait
root       53274  5.3  0.0  384  320      - A      May 29 100517:09 wait
root       20778  5.2  0.0  384  384      - A      May 29 100164:11 wait
lisadm   2224370  1.4  1.0 42264 87844      - A      Oct 20 3555:30 /dbms/sybase15/ebf15090s/ASE-15_0/bin/dataserver -ONLINE:1,0,0xffff
lisadm   2191414  1.1  1.0 43484 89064      - A      Oct 20 2785:13 /dbms/sybase15/ebf15090s/ASE-15_0/bin/dataserver -d/dev/rlisqeh_sp1
lisadm   1052712  0.8  1.0 65480 111060      - A      Sep 14 5559:22 /dbms/sybase15/ebf15090s/ASE-15_0/bin/dataserver -d/dev/rlisqeh_sp9
lisadm    455004  0.8  1.0 42060 87640      - A      Sep 22 4480:23 /dbms/sybase15/ebf15090s/ASE-15_0/bin/dataserver -ONLINE:1,0,0xffff
lisadm    958672  0.8  1.0 43604 89184      - A      Sep 14 5093:08 /dbms/sybase15/ebf15090s/ASE-15_0/bin/dataserver -d/dev/rlisqeh_sp7
lisadm   1036316  0.7  1.0 43328 88908      - A      Sep 22 4272:43 /dbms/sybase15/ebf15090s/ASE-15_0/bin/dataserver -d/dev/rlisqeh_sp3
lisadm   1081516  0.5  1.0 71296 116876      - A      Sep 14 3682:07 /dbms/sybase15/ebf15090s/ASE-15_0/bin/dataserver -ONLINE:1,0,0xffff
lisadm   2228680  0.4  1.0 43172 88752      - A      Oct 22 1038:12 /dbms/sybase15/ebf15090s/ASE-15_0/bin/dataserver -d/dev/rlisqeh_sp5
lisadm    884808  0.4  1.0 43392 88972      - A      Sep 14 2655:01 /dbms/sybase15/ebf15090s/ASE-15_0/bin/dataserver -d/dev/rlisqeh_sp6
testadm   504124  0.2  1.0 43064 88644      - A      Aug 04 2712:21 /dbms/sybase15/ebf15090s/ASE-15_0/bin/dataserver -sLIS_QEH_SD9 -d/d
prodlib  1704036  0.2  0.0 5876 8764      - A    13:31:05  4:17 rcs_work -i/appl/lis/home/lib/prod/work/rcs_1.ini -l1 -g/appl/lis/h
testadm   336112  0.2  1.0 42928 88508      - A      Aug 04 1908:47 /dbms/sybase15/ebf15090s/ASE-15_0/bin/dataserver -sLIS_QEH_SD1 -d/d
patrol    332090  0.2  0.0 47892 48488      - A      Jun 04 3000:11 PatrolAgent -p 3181 -id qehibm72
testadm   340182  0.1  1.0 42772 88352      - A      Aug 04 1556:01 /dbms/sybase15/ebf15090s/ASE-15_0/bin/dataserver -sLIS_QEH_SD7 -d/d
prodlib  2232824  0.1  0.0 5904 8796      - A    13:31:05  3:00 rcw_work -i/appl/lis/home/lib/prod/work/rcw_1.ini -l1 -g/appl/lis/h
testadm   713030  0.1  1.0 42932 88512      - A      Aug 04 1350:45 /dbms/sybase15/ebf15090s/ASE-15_0/bin/dataserver -sLIS_QEH_SD3 -d/d
prodloe  2138310  0.1  1.0 89960 89996      - A    12:55:04  2:51 java dom.loeReceiver -sMQSENDER -i/appl/lis/home3/prodloe/work/LOEM
testadm   372960  0.1  1.0 43136 88716      - A      Jul 29 1188:54 /dbms/sybase15/ebf15090s/ASE-15_0/bin/dataserver -d/dev/rlisqeh_st9
prodlib  1036582  0.1  0.0 5852 8740      - A    13:31:00  2:22 rcs_work -i/appl/lis/home/lib/prod/work/rcs_3.ini -l3 -g/appl/lis/h
testadm   250074  0.1  1.0 42784 88364      - A      Aug 04 976:12 /dbms/sybase15/ebf15090s/ASE-15_0/bin/dataserver -sLIS_QEH_SD5 -d/d
testadm   401610  0.1  1.0 42772 88352      - A      Aug 04 852:50 /dbms/sybase15/ebf15090s/ASE-15_0/bin/dataserver -sLIS_QEH_SD6 -d/d

Thank you for your help
# 6  
Old 11-14-2008
Ok, it's just a little bit more complicated then a oneliner:

Code:
ps auxwww | head -30 |
awk 'NR==1{$1=$1;print;next} {
for(i=1;i<=NF;i++){
  if(i==9) {
    if(int($i)>0) {
      s=s" | " $i " | " $++i " | " $++i
    } else {
      s=s" | "$i " " $++i " | " $++i " |"
    }
  }
  else if(i>11) {
    s=s" "$i
  }
  else {
    s=s?s" | "$i:$i
  }
}
{print s;s=""}
}'

If you get errors use nawk or /usr/xpg4/bin/awk on Solaris

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting a field without disturbing field separator on other fields

Hi All, I have the input as below: cat input 032016002 2.891 97.109 16.605 27.172 24.017 32.207 0.233 0.021 39.810 0.077 0.026 19.644 13.882 0.131 11.646 0.102 11.449 76.265 23.735 16.991 83.009 8.840 91.160 0.020 99.980 52.102 47.898 44.004 55.996 39.963 18.625 0.121 1.126 40.189... (15 Replies)
Discussion started by: am24
15 Replies

2. Shell Programming and Scripting

Field separator

Hello All, I have a file, but I want to separate the file at a particular record with comma"," in the line Input file APPLE6SSAMSUNGS5PRICEPERPIECEDOLLAR600EACH010020340URX581949695US to Output file APPLE6S,SAMSUNGS5,PRICEPERPIECE,DOLLAR600EACH,010020340URX581949695,US This is for... (11 Replies)
Discussion started by: m6248m
11 Replies

3. UNIX for Dummies Questions & Answers

change field separator only from nth field until NF

Hi ! input: 111|222|333|aaa|bbb|ccc 999|888|777|nnn|kkk 444|666|555|eee|ttt|ooo|ppp With awk, I am trying to change the FS "|" to "; " only from the 4th field until the end (the number of fields vary between records). In order to get: 111|222|333|aaa; bbb; ccc 999|888|777|nnn; kkk... (1 Reply)
Discussion started by: beca123456
1 Replies

4. Shell Programming and Scripting

Gawk field separator problem

I'm trying to run a code using gawk and I'm having some trouble with it. What I'm trying to do is pull out value11 from the following input: value11,value12,value13 value21,value22,value23 I have successfully done this before using awk with the following code: awk 'NR == 1 {FS=",";... (4 Replies)
Discussion started by: KomjongShawn
4 Replies

5. Shell Programming and Scripting

Strings as Field separator

Hi, How i can use two strings as field separator.. I want to use filed separator's as &lt; and &gt; input - shdhd ads&lt;adsd adfs &gt;sdfsd sfsdfsd&lt; Please help me in this..:wall: thanks a lot... (3 Replies)
Discussion started by: pamu
3 Replies

6. Shell Programming and Scripting

echo field separator

I am trying to echo all fields except for the last field. I want to include the field seperator, but it is removed. echo "a;s;v;g" | awk -F ";" '{$(NF--)=""; print}' a s v I want an output like this: a;s;v; (3 Replies)
Discussion started by: locoroco
3 Replies

7. Shell Programming and Scripting

Field separator X'1F'

Hi, I have a flat file with fields separated by a X'1F' i have to fetch 4th field from second line. please help me how to achieve it. I tried with below command and its not working. cut -f4 -d`echo -e '\x1f'` filename.txt I am using SunOS. Thanks in advance. (2 Replies)
Discussion started by: rohan10k
2 Replies

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

9. Shell Programming and Scripting

Field separator Ques.

Hello... Im trying to use "- " as field separator... I used awk -F"- " '{print $3}' input_file ... but it's not working, it assumes that the field separator is "-" and not "- " ... Any ideas ?? :( Thanks (6 Replies)
Discussion started by: yahyaaa
6 Replies

10. Shell Programming and Scripting

field separator as regexp

I have some version of AWK that does not support regular expression field separators ( neither do I have nawk or gawk). How do I go about reading a line with the field separator as either the string "=#" or "+=". My data looks like this: abhishek=#nnnnn+#1234+#87 One option is to use... (2 Replies)
Discussion started by: Abhishek Ghose
2 Replies
Login or Register to Ask a Question