UNIX ksh - To print the PID number and repeat count

Tags
aix, grep, shell, sorting, uniq, unix

 
Thread Tools Search this Thread
# 1  
Old 03-24-2018
UNIX ksh - To print the PID number and repeat count

This question is asked in an interview today that I have to return output with each PID number and the count of each PID number logged today. Here is the script that I have written. Can you confirm if that would work or not. The interviewer didn't said if my answer is correct or not. Can someone review this for me. They want me to print each unique PID:number and its count with tab space like PID:5674 10

Log file abc.log

Code:
PID:6543 ……    
…………………
PID:4325 ……
……………………
PID:6543 ……


Log file xyz.log

Code:
PID:8888 ……
…………………
PID:9992 ……
……………………
PID:6543 ……

Note: The PID numbers can repeat in a file. And also one PID number can appear in multiple log files.

— If today’s and previous day’s log files are in same folder

Code:
#!/bin/ksh

cd /A/B/ 
for a in `ls -lrt | grep "Mar 24" | awk '{print $9}'`;    — list of files generated today
do 
grep "^PID:" $a | cut -d " " f1  >> /tmp/abc.log   — saving first column which look like PID:23456 
done

for b in `cat /tmp/abc.log | sort -u`;
do
x=grep $b /tmp/abc.log | grep -v grep | wc;
echo $b"    "$x    — will print like PID:23456  10(count)
done


— If today’s log files are in different folder
Code:
#!/bin/ksh

cd /A/B/
for a in `ls /A/B/*.log`
do
grep "^PID:" $a | cut -d " " f1 >> /tmp/abc.log
done

for b in `cat /tmp/abc.log | sort -u`;
do
x=grep $b /tmp/abc.log | grep -v grep | wc;
echo $b"    "$x
done


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-24-2018 at 07:29 AM.. Reason: Added CODE tags.
# 2  
Old 03-24-2018
Now - what would be YOUR OWN results running your scripts (which BTW don't really differ except for the ls targets and the date selection)?

I immediately can see two syntax and one semantical errors, and several opportunities for improvements / optimisation. With the errors removed, the scripts should deliver what was requested.

Last edited by RudiC; 03-24-2018 at 07:58 AM..
# 3  
Old 03-24-2018
Hi RudiC,

I don't have unix terminal at the moment so I have not tested it, I can do that on Monday when I reach home. I was curious to know if what I my scripts would work. Can you identify the errors you can see ( is that ; after the for statement in first for loop)
# 4  
Old 03-24-2018
I - and not me alone - would usually recommend to try to find errors or opportunities oneself to
- experience a good learning step.
- become a real IT person (programmer, admin, whatever).

So - how about you check on Monday, and if you don't get it running, you come back to get help? And, it's NOT the ; as this is not necessary on a line break.

You posting the corrected versions here would also be appreciated.
# 5  
Old 03-26-2018
Hi Rudic,

The code is working fine and the output as expected. Thanks for your help on it.
# 6  
Old 05-17-2018
Here is an improved version of your script.
Code:
#!/bin/ksh

cd /A/B/ || exit    # exit if failed
for a in `ls -lrt | awk '/ Mar 24 / {print $9}'`    # list of files generated today
do 
  grep "^PID:" "$a" | cut -d " " f1   # print first column which look like PID:23456
done > /tmp/abc.log   # all output in the for-do-done block goes here

# for each unique PID ...
for b in `sort -u < /tmp/abc.log`   # cat file |  is overhead compared to  < file
do
  x=`fgrep -xc "$b" /tmp/abc.log`    # whole line match! And grep can count
  echo "$b    $x"    # will print like PID:23456  10(count)
done

In principal a $var in a command argument sould be in quotes, like "$var", otherwise the shell tries to expand it (word splitting, globbing).
This is standard shell code. If you know awk and its hashed arrays, then a smart all-in-awk solution comes in sight...

Last edited by MadeInGermany; 05-17-2018 at 02:47 PM..

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
UNIX - command to count number of files in subdirectories ravikirankethe UNIX for Dummies Questions & Answers 4 03-17-2014 05:50 PM
Count and print the number of occurences arch Shell Programming and Scripting 5 07-22-2013 09:22 AM
count number of entries perl program or Unix script manigrover Shell Programming and Scripting 1 05-05-2012 04:33 AM
Help with Unix and Awk to count number of occurrences INHF Shell Programming and Scripting 2 01-25-2012 12:01 AM
What is a PID number? nariangel Red Hat 3 04-17-2011 02:57 PM
How do you print the number of processes that each user is currently running in Unix? Duo11 UNIX for Dummies Questions & Answers 4 03-07-2011 05:28 AM
how to add the number of row and count number of rows juelillo Shell Programming and Scripting 6 07-15-2010 09:52 AM
count identical strings print last row and count dcfargo Shell Programming and Scripting 2 07-07-2010 11:36 AM
print number pyramid with for loop in unix joshilalit2004 Shell Programming and Scripting 12 05-12-2010 01:13 PM
Unix command to count the number of files with specific characters in name murphysm UNIX for Dummies Questions & Answers 6 05-07-2010 05:12 AM
KSH 88 - Can I find the PID for an IP connection? gcraill Shell Programming and Scripting 3 03-01-2010 04:54 AM
pid from port number in AIX solaix14 UNIX for Dummies Questions & Answers 1 12-30-2008 12:51 PM
Repeat previous unix command syang68 UNIX for Dummies Questions & Answers 4 07-07-2008 03:03 PM
How to ignore characters and print only number using unix? cdfd123 UNIX for Dummies Questions & Answers 5 08-03-2007 04:47 AM
In ksh, how does an in-line child sub-process get its own PID? MichLab Shell Programming and Scripting 6 05-27-2006 09:02 AM