awk multiple lines of output - print one line
Hi Folks,
I am running a bi-directional iperf and want just a few fields from the output on a single line. Here is an example of the output from the iperf: Code:
------------------------------------------------------------ Code:
host 919 42.1 Thanks. |
I'm confused do you just want the Bandwidth number printed to the screen?
If so then a simple grep/cut is easier than an awk in my opinion although it can be done by setting your FS and RS with awk. |
Awk has regex and conditional logic (amongst other features) - seems perfect for the job. The OP didn't show the code, but something like this might work
Code:
awk '/\/sec$/ {first? second=$7 : first=$7 ; if (second) {print "host ",first,second ; first=second =""} }' infile |
syg your solution is the more correct one here, but this could be easily accomplished with a much smaller statement:
Code:
grep -i bytes | awk '{print $7" "$8}' It really depends how robust you want to make this, if you are just trying to pull out two numbers for yourself to see using grep/awk combo that makes sense. If you want to cron this or program it into a script then syg00's awk is the more correct method. Its always right to use the best tool for the job. Why type out all of the extra junk if its for a one time use? And why would you not want to use a simple grep/awk pipe for a programmatic task that will be repeated often? Answer: It is much simpler if you have a static set of input that you know exactly what it is and need to one off something quickly while you are sitting at the CLI. However, a simple grep/awk pipe is not reliable if your input changes. |
part of code under C.
Code:
fp = fopen( filetoopen , "rb" ) ; |
I think we need to clarify a few more things here.
1) Is the output static, or are you filtering a continuing stream? 2) What exactly is "host"? It doesn't appear in the input text as shown, so where does it come from? Should it only print once, or is it a stand-in for something that changes in (or depending on) the input? 3) Similarly, do you want all output to appear on a single line, or are there separate lines for different entries, or what? Making the assumption that "host" is a fixed value, and that you want all "KBytes" values to appear after it, I'd probably use something like this: Code:
iperf .... | awk 'BEGIN{ printf "%s","host" } /KBytes/ { printf "%s"," "$7 }' You might also want to add an END section to tack a final newline onto the end. |
Hi Everyone,
Thanks for all of the helpful advice! The content is static, it is the output from a bi-directional iperf. "Host" will be the ip of the system that just ran the iperf. I have an array of IP's that run through a loop and write the output to a file. So the file will look something like: Code:
10.10.10.5 919 42.1 Code:
10.10.10.5 919 42.1 Thanks again for all the help! |
So,
Adding the END section works great if I run it directly from the shell but when I dump it into the bash script as such: Code:
/usr/local/bin/iperf -c $i -d -f KBytes | awk 'BEGIN{ printf "%s","'${i}'"" } /KBytes/ { printf "%s"," "$7} END{ print "" }' >> $file Code:
awk: BEGIN{ printf "%s","19.239.211.30"" } /KBytes/ { printf "%s"," "$7} END{ print "" } Thanks. |
Quote:
Code:
"$7 Also, the extra quote seems wrong: Code:
"19.239.211.30"" |
Thanks,
Here is the final win: Code:
/usr/local/bin/iperf -c $i -d -f KBytes | awk 'BEGIN{ printf "%s","'${i}'" } /KBytes/ { printf "%s"," "$7 } END{ print "" }' >> $file Code:
19.239.243.30 932 19.4 |
All times are GMT -5. The time now is 06:46 AM. |