I am running a script whose output is:

Circuit                           Packets/Bytes Sent Packets/Bytes Received
2/1 vlan-id 1005                         11589119559            14650974869
                                       3084237796552         13027195853643

This script is being run every five minutes, for MRTG purposes. Now I need to get the value of the second row for columns 2 and 3 separately:

Bytes Sent 3084237796552

Bytes Received 13027195853643

How can I do that using sed?

link|improve this question
2  
Why do you want to use sed? – Daniel Beck Dec 22 '11 at 6:17
feedback

2 Answers

One solution using sed:

sed -n '$ { s/^\s*/Bytes Sent /; s/\([0-9]\)[ ]/\1\n/; s/\(\n\)\s*/\1Bytes Received /; p }' infile

Explanation:

-n                               # Disable printing lines.
$                                # In last line...
s/^\s*/Bytes Sent /              # Substitute all spaces from the beginning with literal string 'Bytes Sent'
s/\([0-9]\)[ ]/\1\n/             # Substitute first match of a space after a number with a new line.
s/\(\n\)\s*/\1Bytes Received /   # Substitute all space after the new line with literal string 'Bytes received'
p                                # Print this line (two lines after the included '\n')

Result:

Bytes Sent 3084237796552
Bytes Received 13027195853643
link|improve this answer
feedback

I never got the hang of sed and multiple lines, but if you just want it done this should work.

whatever_gives_your_data.sh | tail -1 |  awk '{ print "Bytes sent "$1 "\nBytes recieved "$2 }'
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.