• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Some Unix Scripting help please...

uCsDNerd

Senior member
Hi Guys, I'm having difficulty on how to do the following:

I've got a LARGE file with stats in the below format. I'd like to check and see if column 3 is equal to 0 and would like to get the next record following that record with a non '0' number in column 3 and add the two values of each record found in their column ones. How is this possible? I've thought of using awk, but can't figure out how to set variables equal to data values while reading line by line. any help is much appreciated! Thanks!

example table:
A B C D E
4322.455 432 0 90090 54900
5663.696 959 0 59206 90230
3902.552 293 41 90509 89289
...

here i'd like to save the value 4322.455 and add it to value 3902.552
 
I don't really understand what you want to do - 😕 - but anyhow, this sounds like a job for a Practical Extraction and Report Language ...
 
i was hoping that i wouldn't have to go for perl, but i guess i gotta --

anyhow, in perl, how can i read in a file line by line and use a delimiter to read each individual value in that line?

Thanks!
 
nah, awk can handle this easily. i dont know much about awk but i still find it invaluable.

cat 5663.696 959 0 59206 90230 | awk '{ print $1 }'

would print

5663.696

cat 5663.696 959 0 59206 90230 | awk '{ print $3 }'

would print

0

etc etc. the $number just goes to the nth word, delimited by spaces and tabs.

oh and why use awk for all of it? just make a shell script for the general stuff (variables, if statements and whatnot) and then use awk for the searching through text.

i might add that perl *would* be awesome at this, but shell scripts are a bit easier to jump into than perl. then again if you have some time on your hands, why not..
 
I'm not entirely clear what you want this script to do, but try this bit of awk:

BEGIN {x1 = "NONE"}
{
if($3 == 0 && x1 == "NONE")
{
x1 = $1
}
else if($3 != 0 && x1 != "NONE")
{
print (x1 + $1)
x1 = "NONE"
}
}


just save this to a file (junk.awk) and execute as follows:

awk -f junk.awk filename
 
Thanks! i'll try out your suggestions and see if i can use them in some way.

Lets see if i can clarify what I need to do--
I have a large file ( 700MB+ ) that is filled with network packet statistics on each line( packet #, time sent, where from, where to, etc. ) . To make the statistics small enough for me to fit into excel, I'd like to use the timestamps to find the number of packets sent in 1000 seconds, add up all of their packet sizes and divide by 1000 to find bandwidth usage within that time span. I'd like to do this for the entire file which runs about 4 or 5 days i think.
Secondly, (in the same large file) I'd like to print out the time difference from when the first packet was sent from here to there, to when the acknowledgement packet was sent back from there to here. This is why I'd like to find out if there is a 1 or a 0 in a specific field of the data.

So now, can awk help me any? or do i have to move on up to perl?

Thanks again!
 
Back
Top