Awk script to sort and display total occurancies


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk script to sort and display total occurancies
# 1  
Old 06-19-2012
Awk script to sort and display total occurancies

Hi

I am not a good awk programer but the below code serving my purpose

Code:
Input
process  12936 user-name   15u  IPv4 7438309      0t0  TCP prdabc.cesi.com:42016->efh.ijk.com:50002 (ESTABLISHED)
process  12942 user-name   13u  IPv4 7764990      0t0  TCP abc.abc.com:9402->192.168.1.1:63919 (ESTABLISHED)
process  12942 user-name   19u  IPv4 7807116      0t0  TCP abc.abc.com:9402->192.168.1.1:63958 (ESTABLISHED)
process  12942 user-name   21u  IPv4 7800224      0t0  TCP abc.abc.com:9402->192.168.1.2:dvt-system (ESTABLISHED)
process  12942 user-name   22u  IPv4 7850124      0t0  TCP abc.abc.com:9402->192.168.1.3:50744 (ESTABLISHED)
process  12942 user-name   23u  IPv4 7800259      0t0  TCP abc.abc.com:9402->192.168.1.2:procos-lm (ESTABLISHED)
process  12942 user-name   24u  IPv4 7800282      0t0  TCP abc.abc.com:9402->192.168.1.2:pda-data (ESTABLISHED)
process  12942 user-name   25u  IPv4 7800285      0t0  TCP abc.abc.com:9402->192.168.1.2:pda-sys (ESTABLISHED)
process  12942 user-name   26u  IPv4 7800319      0t0  TCP abc.abc.com:9402->192.168.1.2:semaphore (ESTABLISHED)
process  12942 user-name   13u  IPv4 7764990      0t0  TCP abc.abc.com:9402->192.168.1.1:63919 (ESTABLISHED)
process  12942 user-name   19u  IPv4 7807116      0t0  TCP abc.abc.com:9402->192.168.1.1:63958 (ESTABLISHED)

The below code Displays number of ips with 90402 port
lsof -i -p 9402 | grep "9402-" | awk '{print $9}' | awk '{FS=":"}{print $2}' | awk '{FS="->"}{print $2}'

The below code is displaying the unique entries
lsof -i -p 9402 | grep "9402-" | awk '{print $9}' | awk '{FS=":"}{print $2}' | awk '{FS="->"}{print $2}'| sort | uniq

But I want to format it something like below
<IP-Address>     <Number of occurrences>
192.168.1.1                     3
192.168.1.2                     2

Any Help is much appreciated
# 2  
Old 06-19-2012
The string of commands contains a flaw, because for the awks FS is not set prior to reading the input (but while on line 1), the field splitting will only be in effect on line 2 and further (and line 3 by the time we get tot the last awk...and the first results will not be printed)

So FS needs to be set beforehand, plus we could combine that grep and all of the awks into one:
Code:
lsof -i -p 9402 | awk -F':|->' '$2==9402{print $3}'

The awk can be changed so that occurrences can be counted like so:
Code:
awk -F':|->' '$2==9402{A[$3]++} END{for(i in A)print i,A[i]}' OFS='\t'

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 06-20-2012
Code:
lsof -i -p 9402 | sed '/9402/!d;s/\(.*\)->\(.*\):\(.*\)/\2/g' | awk '{++a[$1]}END{for(i in a)print i,a[i]}'

This User Gave Thanks to complex.invoke For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to call and sort awk script and output

I'm trying to create a shell script that takes a awk script that I wrote and a filename as an argument. I was able to get that done but I'm having trouble figuring out how to keep the header of the output at the top but sort the rest of the rows alphabetically. This is what I have now but it is... (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. Homework & Coursework Questions

Shell script/awk to sort text

1. The problem statement, all variables and given/known data: I have a file with a fragment of a novel, which I have to clear from punctuation and sort all the words contained one per line and non duplicated, all this going to a file called "palabras". Here is fragment of the input file: ... (4 Replies)
Discussion started by: ektorzoza
4 Replies

3. UNIX for Dummies Questions & Answers

Display total hard disk size?

Hi, Using Redhat, how do I display total hard disk size? I know how to do that in Solaris, you can type, format. It will show how big the disks are. This is what I did so for. $ df -t ext3 -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-LogVol00 ... (1 Reply)
Discussion started by: samnyc
1 Replies

4. Emergency UNIX and Linux Support

Grab total page read from webalizer display in html

Hi, I need a way to grab the total combines since inception, total pages read from webalizer on my centos server or any other location (as long as since inception) and display the result live on my website So with each visit it would be increasing, or perhaps live (ajax) not sure But can... (0 Replies)
Discussion started by: lawstudent
0 Replies

5. Shell Programming and Scripting

Display LineNo Incase Total Number Of Delimiter Does matches in a given variable

I have many files .dat extension. requirement is to display line no if no of delimiter does not matches in a given variable lets say File: REF_BETOS.dat HCPCS_OR_CPT_CODE~BETOS_CODE~TERMINATION_DATE 0001F~Z2~ 0003T~I4~B20061231 0005F~Z2~~~ 0008T~P8~B20061231... (1 Reply)
Discussion started by: ainuddin
1 Replies

6. Shell Programming and Scripting

awk script to calculate total

Hi First field is the Record Type. A Record Type 5 can have multiple Record Type 6's before another Record Type 5 appears. I want to calculate the total of fields at position 8-11 on Record type 6 when Record Type 5 has a field at position 11-14 equals to '2222'. then it should delete the lines... (2 Replies)
Discussion started by: appsguy616
2 Replies

7. Shell Programming and Scripting

sort files by date, delete oldest, if total size bigger than

hello people i need your help please i want to achieve the following with the simplest, most efficient shell-tools: i have a directory with a lot of files from users. the script should check which partition the dir is on if the partition with the directory is more than 90% full ... (2 Replies)
Discussion started by: scarfake
2 Replies

8. Shell Programming and Scripting

Using sort with awk script

I have a file with four fields and an awk script that strips out one field displaying the remaining three. I have added headings for each of these fields such as Player - Year - RBIs then below it comes the data. What I am trying to do is sort the RBIs field in my script from most to least at the... (9 Replies)
Discussion started by: Trellot
9 Replies

9. Shell Programming and Scripting

ls I do not want to display total with ls

I'm trying to write the filenames only to a file. The following line works: ls -l /home/alex >> log/filetrack.log But the file filetrack.log also contains the "total" i.e. the first line is total 3462984 Also, I would like only the filename, date and size to be listed. However, my main... (3 Replies)
Discussion started by: rx82000
3 Replies
Login or Register to Ask a Question