Sponsored Content
Top Forums Shell Programming and Scripting Filter on one column and then perform conditional calculations on another column with a Linux script Post 302939490 by Zooma on Wednesday 25th of March 2015 10:14:08 PM
Old 03-25-2015
Filter on one column and then perform conditional calculations on another column with a Linux script

Hi,
I have a file (stats.txt) with columns like in the example below. Destination IP address, timestamp, TCP packet sequence number and packet length.

Code:
destIP   time  seqNo  packetLength
1.2.3.4  0.01   123       500
1.2.3.5  0.03    44       1500
1.3.2.5  0.08    44       1500
1.2.3.4  0.44   123       500
1.2.3.4  0.48   123       500
1.2.3.4  0.52   124       800
1.2.3.4  0.72   124       800
1.2.3.5  0.83    45       80
...

I'm trying to come up with a way to derive some statistics from this file. Ideally, my Linux script would take the input from stats.txt (which could consist of 10 000's of rows) and tell per destination address (example for address 1.2.3.4 above used to illustrate):

- For destination IP 1.2.3.4, there has been two retransmissions for sequence number 123 and one retransmission for sequence number 124. This means three packet errors in total.
- The time between the first and last packet with the same sequence number is 0:48-0:01=0:47 seconds and 0:72-0:52=0.2 seconds respectively.
- Number of successful packets to 1.2.3.4 is two (sequence number 123 and 124, assuming that 124 is ok since it's not retransmitted).
- The total number of successfully transmitted Bytes to 1.2.3.4 is 500+800=1300B.

And of course the same kind of stats for any other IP address.

My current approach is to first sort the file like this:

Code:
sort -u -k1,1 -k3,3 -k2,2 stats.txt > statsSorted.txt

Then I get this:
Code:
1.2.3.4  0.01   123       500
1.2.3.4  0.44   123       500
1.2.3.4  0.48   123       500
1.2.3.4  0.52   124       800
1.2.3.4  0.72   124       800
1.2.3.5  0.03    44       1500
1.3.2.5  0.08    44       1500
1.2.3.5  0.83    45       80
...

Then to use awk to extract the stats. Have used the approach below to get started but I get syntax errors on pretty much everything. It probably looks quite bad with the nested loops as well. Wonder if someone could give some advice on how to improve the syntax or hints on how to make it work?

Code:
awk '
{	# Do-while criteria: as long as the IP address is the same
	do
		address[$1] = $1
		# Loop as long as sequence number is the same
		do
		
			# Is this the first time we see this sequence number?
			if (!($3 in c))
				# Set temporary min and max time and set retransmission counter to zero.
				tempMin=tempMax=$2
				retransmissions=0
			# If not the first time this sequence number occurs, increment retransmission and add time
			else
3			tempMax=$2
				retransmissions6+
		while ($3 in c)	
		averageTime[$1]=tempMax-tempMin
		retransmissions[$1]=retransmissions
	
	while ($1 in c)
END {	
	for(i in c)
		printf("%-17s %3d %5.1f \n", address[i], averageTime[i], retransmissions[i])
}' statsSorted.txt

Any hits welcome, even on how to form the basic syntax. Then I can try to pull it together myself.

Thanks!
/Z

Last edited by Zooma; 03-25-2015 at 11:15 PM.. Reason: Fixed one typo in the last code section.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to perform calculations using numbers greater than 2150000000.

Could someone tell me how to perform calculations using numbers greater than 2150000000 in Korn Shell? When I tried to do it it gave me the wrong answer. e.g. I have a ksh file with the contents below: --------------------------------- #!/bin/ksh SUM=`expr 2150000000 + 2` PRODUCT=`expr... (3 Replies)
Discussion started by: stevefox
3 Replies

2. Shell Programming and Scripting

Sed or awk script to remove text / or perform calculations from large CSV files

I have a large CSV files (e.g. 2 million records) and am hoping to do one of two things. I have been trying to use awk and sed but am a newbie and can't figure out how to get it to work. Any help you could offer would be greatly appreciated - I'm stuck trying to remove the colon and wildcards in... (6 Replies)
Discussion started by: metronomadic
6 Replies

3. Shell Programming and Scripting

Conditional aggregation and print of a column in file

Hi My input file looks like field1 field2 field3 field4 field5 field1 field2 field3 field4 field5 field1 field2 field3 field4 field5 :::::::::::: :::::::::::: There may be one space of multiple spaces between fields and no fields contains spaces in them. If field 1 to 4 are equal for... (3 Replies)
Discussion started by: bittoo
3 Replies

4. Shell Programming and Scripting

Replace a column with a value conditional on a value in col1

Hi, Perhaps a rather simple problem...? I have data that looks like this. BPC0013 ANNUL_49610 0 0 1 1 BPC0014 ANNUL_49642 0 0 2 1 BPC0015 ANNUL_49580 0 0 1 1 BPC0016 ANNUL_49596 0 0 2 1 BPC0017 VULGO_49612 0 0 1 1 BPC0018 ANNUL_49628 0 0 1 1 BPC0019 ANNUL_49692 0 0 2 1 170291_HMG... (4 Replies)
Discussion started by: genehunter
4 Replies

5. Shell Programming and Scripting

Enter third column & Perform Operation

I am trying to enter a third column in this file, but the third column should that I call "Math" perform a some math calculations based on the value found in column #2. Here is the input file: Here is the desired output: Output GERk0203078$ Levir Math Cotete_1... (5 Replies)
Discussion started by: Ernst
5 Replies

6. Shell Programming and Scripting

awk , conditional involving line and column

Dear All, I indeed your help for managing resarch data file. for example I have, data1.txt : type of atoms z vz Si 34 54 O 20 56 H 14 13 Si 40 17 O ... (11 Replies)
Discussion started by: ariesto
11 Replies

7. UNIX for Dummies Questions & Answers

Command line / script option to filter a data set by values of one column

Hi all! I have a data set in this tab separated format : Label, Value1, Value2 An instance is "data.txt" : 0 1 1 -1 2 3 0 2 2 I would like to parse this data set and generate two files, one that has only data with the label 0 and the other with label -1, so my outputs should be, for... (1 Reply)
Discussion started by: gnat01
1 Replies

8. Shell Programming and Scripting

awk script concatenate two column and perform mutiplication

Need your help in solving this puzzle. Any kind of help will be appreciated and link for any documents to read and learn and to deal with such scenarios would be helpful Concatenate column1 and column2 of file 1. Then check for the concatenated value in Column1 of File2. If found extract the... (14 Replies)
Discussion started by: as7951
14 Replies

9. Shell Programming and Scripting

awk script to append suffix to column when column has duplicated values

Please help me to get required output for both scenario 1 and scenario 2 and need separate code for both scenario 1 and scenario 2 Scenario 1 i need to do below changes only when column1 is CR and column3 has duplicates rows/values. This inputfile can contain 100 of this duplicated rows of... (1 Reply)
Discussion started by: as7951
1 Replies

10. Shell Programming and Scripting

Conditional Column Value

Hi Folks, I'm trying tog ain further experience with shell programming and have set my a small goal of writing a little filesystem monitoring script. So far my output is as follows: PACMYDB03 Filesystem Size Used Avail Use% Status /usr/local/mysql/data ... (5 Replies)
Discussion started by: Axleuk
5 Replies
All times are GMT -4. The time now is 12:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy