Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

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

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
share|improve this answer
add comment

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 }'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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