Awk - Compare fields and increment variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk - Compare fields and increment variables
# 8  
Old 05-22-2009
Understood.

One thing I may be able to do is sort the packets first by order of packet size, making sure they are still in order of time received after that.


Thanks for confirming.


Mario
# 9  
Old 05-26-2009
I'm trying to understand better what your script does so I can manipulate it to do an additional check on the first field (I want to ensure the lines been compared have the first field within 5 integers of each other).

I'd like to work it out myself, but just need a bit of help with understanding the syntax below...


So, in the part:

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


I understand you set the variables last and ip to $12 and $4 respectively, but I'm not sure what the (!last) check does..

Is it "if last exists"?


I get the 'else' to be:

{
if (last==$12) ipc[ip]++
last=0
}


"if last is the same as the 12th field, increment the the ip counter"


Please excuse this 'silly' question, but I'm really trying to get my head around this and not finding it easy.

Regards,
# 10  
Old 05-26-2009
Quote:
Originally Posted by mv652
awk '
$1 ~ /^[0-9]/ {
if (!last) { last=$12; ip=$4 }

I understand you set the variables last and ip to $12 and $4 respectively, but I'm not sure what the (!last) check does..
I'm setting the variable 'last' to the value in the 12th field, but only if I've not set this variable before or if the variable contains the value 0. That's what 'if (!last)' means, if the 'last' variable is blank or 0. In AWK, if you reference a variable that's never been set before, it's the same as if it were blank or 0.

Quote:
Originally Posted by mv652
I get the 'else' to be:

{
if (last==$12) ipc[ip]++
last=0
}


"if last is the same as the 12th field, increment the the ip counter"
Well ipc[] is an associative array. The key is the 'ip' variable set earlier. Yes, we're maintaining counters for each unique IP address we come across where the 12th field is the same on two consecutive lines, and this is where that counter is incremented.
# 11  
Old 05-26-2009
Ok, great.

Thank you for taking the time to answer.


Regards,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 files and find missing fields awk

Hello experts! I have 2 files. file1 is a list file containing uniquely names. e.g.: name1 number number name2 number number name5 number number name10 number number ... file2 is a data file arbitrary containing the names of file1 in paragraphs separated by "10" e.g. name4 ... (3 Replies)
Discussion started by: phaethon
3 Replies

2. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

3. Shell Programming and Scripting

awk question ? set 2 variables by matching fields

Hello, I'm trying to get the TOP and BASE numbers printed out File looks like this: 2300 CAR # 2300 is the TOP 2310 CAR 2335 CAR 2455 CAR # 2455 is the BASE 1000 MOTOR # 2455 will become this TOP 2000 MOTOR 3000 MOTOR 4000 MOTOR # 4000 is the BASE 2345 BIKE # 4000... (8 Replies)
Discussion started by: charlieglen
8 Replies

4. UNIX for Dummies Questions & Answers

Compare values of fields from same column with awk

Hi all ! If there is only one single value in a column (e.g. column 1 below), then return this value in the same output column. If there are several values in the same column (e.g. column 2 below), then return the different values separated by "," in the output. pipe-separated input: ... (11 Replies)
Discussion started by: lucasvs
11 Replies

5. Shell Programming and Scripting

awk (or other) script that assigns fields from a line to multiple variables

Hey all, Unfortunately I have only basic knowledge of awk and/or scripting. If I have a file with lines that can look similar to this: Name=line1 Arg1=valueA Arg2=valueB Arg3=valueC Name=line2 Arg1=valueD Name=line3 Arg1=valueE Arg3=valueF Name=line4 Arg2=valueG ... (4 Replies)
Discussion started by: Rike255
4 Replies

6. Shell Programming and Scripting

using awk compare two variables in bash

ok this is probably going to turn out to be something really stupid but i've tried to use the following command in a script but the output is just a blank screen and i have to use Ctrl c to exit it. awk 'BEGIN {printf "%.2f\n", '${bashArray}'>='$Variable' {print '${bashArray}'}}' the command... (2 Replies)
Discussion started by: zagreus360
2 Replies

7. Shell Programming and Scripting

awk to compare diff output by fields

Diff output as follows: < AAA BBB CCC DDD EEE 123 > PPP QQQ RRR SSS TTT 111 > VVV WWW XXX YYY ZZZ 333 > AAA BBB CCC DDD EEE 124 How can i use awk to compare the last field to determine if the counter has increased, and need to ensure that the first 4 fields must have the same... (15 Replies)
Discussion started by: ux4me
15 Replies

8. Shell Programming and Scripting

AWK Compare files, different fields, output

Hi All, Looking for a quick AWK script to output some differences between two files. FILE1 device1 1.1.1.1 PINGS device1 2.2.2.2 PINGS FILE2 2862 SITE1 device1-prod 1.1.1.1 icmp - 0 ... (4 Replies)
Discussion started by: stacky69
4 Replies

9. Shell Programming and Scripting

Compare fields in 2 files using AWK

Hi unix gurus, I have a urgent requirement, I need to write a AWK script to compare each fields in 2 files using AWK. Basically my output should be like this. file1 row|num1|num2|num3 1|one|two|three 2|one|two|three file2 row|num1|num2|num3 1|one|two|three 2|one|two|four ... (5 Replies)
Discussion started by: rashmisb
5 Replies

10. Shell Programming and Scripting

Need awk script to compare 2 fields in fixed length file.

Need a script that manipulates a fixed length file that will compare 2 fields in that file and if they are equal write that line to a new file. i.e. If fields 87-93 = fields 119-125, then write the entire line to a new file. Do this for every line in the file. After we get only the fields... (1 Reply)
Discussion started by: Muga801
1 Replies
Login or Register to Ask a Question