The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare fields in a file with duplicate records rleal Shell Programming and Scripting 1 01-29-2009 02:55 AM
How to read and compare multiple fields in a column at the same time ahjiefreak Shell Programming and Scripting 1 06-19-2008 12:08 PM
Compare 2 files through multiple fields newinawk Shell Programming and Scripting 4 06-12-2008 05:34 PM
Compare two arrays in sh or compare two fields rijeshpp Shell Programming and Scripting 0 10-31-2007 02:47 AM
Passing specific fields from files as variables keladar UNIX for Dummies Questions & Answers 4 04-13-2005 07:00 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-22-2009
mv652 mv652 is offline
Registered User
  
 

Join Date: May 2009
Posts: 14
Awk - Compare fields and increment variables

Hi,

My first post to this group...

I have a need to to parse a source file which is a capture from a network analyser.

I have two fields that need to be checked:
- Field 7 represents the packet length (an integer), and
Field 4 represents a network address (e.g. 192.168.25.3)

- The first check is to find 2 consecutive lines that have the same integer in Field 7 i.e. the same length. Original file may not always have these lines consecutive though, but I am ok to ignore those lines if it is too difficult to include those.
- Then, once we have these two lines, check the text in Field 4 for these lines and inidicate the value within the text that is 'first' and increment a variable.

What I'm after is to understand how many times address A is first compared to address B.


My expected output from the sample below would be:

"239.25.30.25 is first once" and "239.25.30.26 is first twice.

Even an output like "239.25.30.25 - 1, 239.25.30.26 - 2" would be great.


Example source:


No. Time Source Destination Protocol Info Length
1 20:44:19.525910000 192.168.30.25 239.25.30.25 UDP Source port: dnp Destination port: 20000 94
2 20:44:19.525932000 192.168.30.26 239.25.30.26 UDP Source port: dnp Destination port: 20000 94
3 20:44:19.525989000 192.168.30.26 239.25.30.26 UDP Source port: dnp Destination port: 20000 114
4 20:44:19.526037000 192.168.30.25 239.25.30.25 UDP Source port: dnp Destination port: 20000 114
13 20:44:19.693262000 192.168.30.26 239.25.30.26 UDP Source port: dnp Destination port: 20000 193
14 20:44:19.693295000 192.168.30.25 239.25.30.25 UDP Source port: dnp Destination port: 20000 193



I believe Awk should be able to take of this, but my awk skills are not good enough to come up with something decent.


I hope someone may be able to point me in the right direction.

Thanks,
Mario
  #2 (permalink)  
Old 05-22-2009
cambridge cambridge is offline
Registered User
  
 

Join Date: May 2009
Posts: 55

Code:
awk '
    $1 ~ /^[0-9]/ {
        if (!last) { last=$12; ip=$4 }
        else
        {
            if (last==$12) ipc[ip]++
            last=0
        }
    }

    END { for (ip in ipc) print ip, ipc[ip] }
' inputfile

produces the following output from your example file:
239.25.30.25 1
239.25.30.26 2
  #3 (permalink)  
Old 05-22-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
somethng like this you can try


Code:
awk 'BEGIN { prev=0 ; count=1 } { if ( prev==$NF) count++;else { count=1;;prev=$NF } print $4,"-", $12 "- " count}'  File.txt

hope tht the last column is sorted
  #4 (permalink)  
Old 05-22-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,406
i couldn't understand your problem(req) fully but i tried this..

Code:
awk 'FNR%2{var=$NF;next}{if(var==$NF){if($4=="239.25.30.26"){v25 += 1}else{v26 += 1}}}END{print "239.25.30.25 - "v25"\n239.25.30.26 - "v26}' filename

  #5 (permalink)  
Old 05-22-2009
mv652 mv652 is offline
Registered User
  
 

Join Date: May 2009
Posts: 14
Thanks

That's great, you've given me what I needed!!

Thank you so much for replying.


Best Regards,
Mario
  #6 (permalink)  
Old 05-22-2009
mv652 mv652 is offline
Registered User
  
 

Join Date: May 2009
Posts: 14
vidyadhar85,

To answer your question, I have a text file containing data from a network capture.

The data is duplicated (on purpose) and is sent to two destinations (multicast addresses). Sometimes data for one destination is received first, other times data to the other destination is first.

I'm trying to work out which destination is usually first depending on the sample I capture.

I've just seen that depending on which awk string I run from the replies above, I get different output / results from the replies received, so will probably still need to verify which gives me the most correct answer for a particular sample.


I think cambridge's script works best for me so far.


Thanks again.

Mario
  #7 (permalink)  
Old 05-22-2009
cambridge cambridge is offline
Registered User
  
 

Join Date: May 2009
Posts: 55
Note that my script only works with consecutive lines. It gets more convoluted if you want to handle other cases, as you'll need to decide how many lines should be allowed between each for it to be a valid sample.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0