Scripting on AIX with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scripting on AIX with awk
# 1  
Old 02-05-2010
Scripting on AIX with awk

Hi,
I am very new to scripting, but I am very keen to learn.

Right now, I have an input file containing the output of ps -emo THREAD (in AIX) where the lines look something like this:
Code:
 
    USER    PID   PPID      TID ST  CP PRI SC    WCHAN        F     TT BND COMMAND
    root      1      0        - A    0  60  1        -   200003      -   - /etc/init
       -      -      -     4099 S    0  60  1        -   410410      -   - -
    root  61688      1        - A    0  60  1  117cf08   240001      -   - /usr/ccs/bin/shlap64
       -      -      -    86067 S    0  60  1  117cf08      400      -   - -
    root  69754      1        - A    0  60 14        *   240001      -   - /usr/sbin/syncd 60
       -      -      -    90307 S    0  60  1 f10001001b3e4730   410410      -   - -
       -      -      -   114837 S    0  60  1 f100010018bc4930   410410      -   - -
       -      -      -   147591 S    0  60  1 f10001001cd94cb0   410410      -   - -
       -      -      -   151687 S    0  60  1 f10001001b54c330   410410      -   - -
       -      -      -   155891 S    0  60  1        -  2400400      -   - -

I am trying to format this to an output file so that all threads (TID) have the process id they belong to in the 2nd field.

My idea is:
1) If $2 is not '-', then
a)send the whole line to the output file and
b)store the value of $2 in a variable $mt that can be used when the next line is read
2) Read the next line
a)if $2 is not '-', then send the whole line to the output file and
b)if $2 is '-', then replace this with the value of $mt taken from the previous line and write to the output file
c)in case of b) reconfirm the same value for $mt, as there may be more threads to the same pid to read.

This is what I have done:
Code:
#!/usr/bin/ksh
ifile="logs/all_THREAD_procs"
ofile="all_THREAD_procs_f"
while read line
do
        awk '{ if ( $2 != "-" ) {
                print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
                else    print $2 }
        }'
done < $ifile > ofile

If I comment out the else line, then it prints all the lines where $2 is not - to the output file, but when I run it with the else condition I get:
Code:
Syntax Error The source line is 3.
 The error context is
                         >>>    else <<<        print $2 }

I can´t find any examples of what I am trying to do, although I imagine it´s a simple mistake to spot for those of you that use awk regularly..

Thanks in advance!

Teresa.

Last edited by pludi; 02-05-2010 at 07:43 AM.. Reason: code tags, please...
# 2  
Old 02-05-2010
Code:
  } else { print $2 }

# 3  
Old 02-05-2010
Doh!

Thank you! Smilie)

T.
# 4  
Old 02-05-2010
This would make it work:
Code:
#!/usr/bin/ksh
ifile="logs/all_THREAD_procs"
ofile="all_THREAD_procs_f"
while read line
do
           awk '{ if ( $2 != "-" )
                       print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
                       else    print $2
                  }' 
done < $ifile > $ofile

But the read loop is superfluous. You could just use:
Code:
ifile="logs/all_THREAD_procs"
ofile="all_THREAD_procs_f"
awk '{ if ( $2 != "-" ) print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
       else print $2
     }' $ifile > $ofile

The awk could perhaps be shortened to:
Code:
awk '$2=="-"{$0=$2}$1=$1' $ifile > $ofile

Or keeping the spacing intact:
Code:
awk '$2=="-"{$0=$2}1' $ifile > $ofile


Last edited by Scrutinizer; 02-05-2010 at 10:29 AM..
# 5  
Old 02-05-2010
I definitely have to work on shortening my script. Thanks for the tip, Scrutinizer! Smilie)

However, either way I run it, it doesn´t do what I need it to do. Back to the file:

Code:
USER    PID   PPID      TID ST  CP PRI SC    WCHAN        F     TT BND COMMAND
    root      1      0        - A    0  60  1        -   200003      -   - /etc/init
       -      -      -     4099 S    0  60  1        -   410410      -   - -
    root  61688      1        - A    0  60  1  117cf08   240001      -   - /usr/ccs/bin/shlap64
       -      -      -    86067 S    0  60  1  117cf08      400      -   - -

I have a script that will be run beforehand to identify "problematic" transactions (definition of "problematic" is irrelevant for now). So, I will have a transaction id (TID) that I will use to grep this file with all master processes and their transactions, to find the master process the problematic transaction belongs to. Lets suppose the bad TID is 86067. The 2nd field in the file would identify the PID, but notice it has a '-'. The PID for this thread would be the PID number in the above line. Sometimes there´ll be many transactions (pid='-') for a master process.

Unless we can read "upwards" to the nearest pid number, I would like to format the file with all the processes, so that all transactions "inherit" the pid number in the second field instead of having a '-'.

Hope this is a little clearer. If you have a better idea for doing this rather than formating the file so I can grep by the transation id and then read the 2nd field, I am all ears! Smilie)

Teresa.

Last edited by pludi; 02-05-2010 at 10:27 AM.. Reason: code tags, please...
# 6  
Old 02-05-2010
Hi, Teresa, you mean something like this?
Code:
ifile="logs/all_THREAD_procs"
ofile="all_THREAD_procs_f"
TID=147591
awk '$2!="-"{p=$2}$4==t{print p}' t=$TID $ifile

output:
Code:
69754

# 7  
Old 02-09-2010
Wow! Thanks, Scrutinizer, that worked. It has taken me a while to incorporate it into my script where it had to read the TID value from a file generated in a previous function. I have so much to learn... Smilie)

T.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in scripting in AIX

Hello, In a loop I am using below command for every database crsctl status res | grep -E "ora\.$DATABASE\.(.+)\.svc" and below is one of the sample output NAME=ora.sgraphut.sgraphutxdb.svc Now I want to extract just service name out of this string (that is sgraphutxdb) please help me how... (1 Reply)
Discussion started by: Vishal_dba
1 Replies

2. Shell Programming and Scripting

AIX 5.3 ksh Scripting

Hi, I posted a request for sever OS types earlier, but got no response. In an attempt to at least have a starting point, I think scaling it to one OS is preferred. Once I see the gist of it I can modify to account for different cases. I need a script that will go and check to see if an LDAP... (2 Replies)
Discussion started by: tekster2
2 Replies

3. Shell Programming and Scripting

help in ksh scripting in aix

Hello gurus I am looking for a script : We need to generate a file list created by user id on a AIX box. Criteria 1: other than userid : dwimpid & aiadmin Criteria 2: Files older than 2 months ( it can be any user id ). File Path to Look: /project and /project1 Thx silu (7 Replies)
Discussion started by: silu
7 Replies

4. Shell Programming and Scripting

Need help in writing AIX ksh scripting

I need to write the script for the below constraints. Need your help urgently The PATH environment variable must conform to the following: • World-writeable directories (/tmp, /var/tmp etc) must not be placed in the PATH variable. • System directories must always be placed before local... (1 Reply)
Discussion started by: kmvinay
1 Replies

5. Shell Programming and Scripting

Help with ksh scripting in AIX

I am looking for a script which does the following Script will run daily. 1.It will get snapshot off all filesystems including nfs mounts, automounts and clearcase mounts. 2.Then it will compare new snapshot with the snapshot created in the previous run. 3.If filesystem exists in... (20 Replies)
Discussion started by: saidiya
20 Replies

6. AIX

Scripting AIX date output !!

Hi, I am writing a korn shell script in AIX. I want to pass the date as a parameter to script and inside it should convert the format to +%a" "%h" "%d" "%Y. For e.g., a parameter passed as "20100301" should be converted to "Mon Mar 01 2010". I can easily do this with AIX "date" option for... (2 Replies)
Discussion started by: shibajighosh
2 Replies

7. Shell Programming and Scripting

Scripting on AIX and using nohup

ok... want to start off by saying i am sorry for the ignorance... still trying to figure out the diffs between solaris and aix... and i think that is where i am running into problems... :o SITTIATION: we are doing an implementation for Oracle's CC&B... we recently switched from... (1 Reply)
Discussion started by: Dagaswolf
1 Replies

8. AIX

scripting routing changes on aix 4.3 and 5x

Hi All, I need to script a routing changes on my aix servers. I can't use smitty, very slow. I need to delete the static routing and add a new one. I also need to change default routing. I know using route add and route delete but I think I remember that this is not enough that's why I... (2 Replies)
Discussion started by: itik
2 Replies

9. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

10. Shell Programming and Scripting

scripting guru's pls help me with scripting on AIX

can someone pls help me with the script for a files coming from one system to a particular directory and i want to write a script to move those files to another directory on different system by renaming the files... pls someone help me on this... thanking in anticipation.... (1 Reply)
Discussion started by: thatiprashant
1 Replies
Login or Register to Ask a Question