UNIX ksh - To print the PID number and repeat count


 
Thread Tools Search this Thread
Operating Systems AIX UNIX ksh - To print the PID number and repeat count
# 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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

UNIX - command to count number of files in subdirectories

I have a folder named test/ and under that I have multiple directories and in each of the directory I have multiple log files. I want to know how many files exists under each sub directory. test |--quanrantine |--logfile1 |--logfile2 |--spooling |--logfile1 ... (4 Replies)
Discussion started by: ravikirankethe
4 Replies

2. Shell Programming and Scripting

Count and print the number of occurences

I have some files as shown below GLL ALM 654-656 654 656 SEM LYG 655-657 655 657 SEM LYG 655-657 655 657 ALM LEG 656-658 656 658 ALM LEG 656-658 656 658 ALM LEG 656-658 656 658 LEG LEG 658-660 658 660 LEG LEG 658-660 658 660 The value of GLL is... (5 Replies)
Discussion started by: arch
5 Replies

3. Shell Programming and Scripting

count number of entries perl program or Unix script

Hi I have a file with number of entries name 1 123 name 1 345 name 1 65346 name2 3243 name2 24234 name 2 234234 so on ......... how to count total number of entries for name 1 and name2...and so on Please guide. (1 Reply)
Discussion started by: manigrover
1 Replies

4. Shell Programming and Scripting

Help with Unix and Awk to count number of occurrences

Hi, I have a file (movies.sh), this file contains list of movies such as I want to redirect the movies from movies.sh to file_to_process to allow me process the file with out losing anything. I have tried Movies.sh >> file_to_process But I want to add the row number to the data... (2 Replies)
Discussion started by: INHF
2 Replies

5. UNIX for Dummies Questions & Answers

How do you print the number of processes that each user is currently running in Unix?

Ok, so I know there's a way to do this, but I've been trying to find out all afternoon with no luck. I think it should print out something like this: 1 bin 2 daemon 6 duo Where the numbers on the left are the number of processes being run by the user whose name is listed on the right. Is... (4 Replies)
Discussion started by: Duo11
4 Replies

6. Shell Programming and Scripting

count identical strings print last row and count

I have a sorted file like: Apple 3 Apple 5 Apple 8 Banana 2 Banana 3 Grape 31 Orange 7 Orange 13 I'd like to search $1 and if $1 is not the same as $1 in the previous row print that row and print the number of times $1 was found. so the output would look like: Apple 8 3 Banana... (2 Replies)
Discussion started by: dcfargo
2 Replies

7. Shell Programming and Scripting

print number pyramid with for loop in unix

How can I print number pyramid with for loop(not while only for) in unix like: 1 22 333 4444 55555 ---------- Post updated at 09:09 AM ---------- Previous update was at 09:07 AM ---------- I forgot it is in ksh...I wrote a script in bash but it is nt wrkng in ksh... bash script... (12 Replies)
Discussion started by: joshilalit2004
12 Replies

8. UNIX for Dummies Questions & Answers

Unix command to count the number of files with specific characters in name

Hey all, I'm looking for a command that will search a directory (and all subdirectories) and give me a file count for the number of files that contain specific characters within its filename. e.g. I want to find the number of files that contain "-a.jpg" in their name. All the searching I've... (6 Replies)
Discussion started by: murphysm
6 Replies

9. Shell Programming and Scripting

KSH 88 - Can I find the PID for an IP connection?

Hi, If I use this command netstat | grep "1268" it shows me all IP addresses connected via port 1268, which is half of what I want. I would like to be able to then map these against a PID on the system, and also thereby get the userid. I have done a couple of days google bashing but... (3 Replies)
Discussion started by: gcraill
3 Replies

10. UNIX for Dummies Questions & Answers

How to ignore characters and print only number using unix?

say D45H E67H G779K F8888U T66Y Y333U output shud be like 45 67 779 8888 66 333 (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question