404Cat
404Cat

Reputation: 143

Line/string manipulation

I have been tremendously helped/taught by the community here many times when it comes to regex/perl/string manipulation. Here's the text:

===================================
| IO Statistics                   |
|                                 |
| Interval size: 2.254 secs (dur) |
| Col 1: Frames and bytes         |
|---------------------------------|
|                |1               |
| Interval       | Frames | Bytes |
|---------------------------------|
| 0.000 <> 2.254 |     10 |  1709 |
===================================

What I want:

Time: 2.254 Seconds
Frames: 10
Bytes: 1709

Any help? Thanks!!!

Upvotes: 1

Views: 88

Answers (3)

mklement0
mklement0

Reputation: 437100

A pragmatic solution:

tail -n 2 file |
  awk '{ printf "Time: %s seconds\nFrames: %s\nBytes: %s\n", $4, $6, $8; exit }'
  • Assumes that the line of interest is the second-to-last input line.
  • Extracts the 4th, 6th and 8th field from that line and formats the output with printf.

Upvotes: 0

dlamblin
dlamblin

Reputation: 45321

Kind of ambiguous as to what to do with other inputs, but in perl (on the command-line as a ~one-liner):

#| 0.000 <> 2.254 |     10 |  1709 |
perl -ie '
if (m:^\|\s+(\d+\.\d+)\s+<>\s+(\d+\.\d+)\s+\|\s+(\d+)\s+\|\s+(\d+)\s+\|:){
  print "Time: ".($2-$1)." Seconds\nFrames: $3\nBytes: $4\n\n"
}' infile

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881133

For that specific text, you can use:

 pax> awk '
 ...>   $6=="(dur)" {print "Time: "$4" Seconds"}
 ...>   $3=="<>"    {print "Frames: "$6"\nBytes: "$8}' inFile

Time: 2.254 Seconds
Frames: 10
Bytes: 1709

That's getting the duration from the Interval size line and the other two figures from the line containing <>.

If your requirements are more complex than that, you'll need to spell them out. For example, let's say you want to get the information from the <> lines from the following (slightly modified) file:

pax> cat infile
===================================
| IO Statistics                   |
|                                 |
| Interval size: 5.000 secs (dur) |
| Col 1: Frames and bytes         |
|---------------------------------|
|                |1               |
| Interval       | Frames | Bytes |
|---------------------------------|
| 0.000 <> 1.234 |     10 |  1234 |
| 1.234 <> 2.718 |     20 |  9876 |
| 2.718 <> 3.142 |     42 |    42 |
| 3.142 <> 5.000 |     99 |    97 |
===================================

You could then use something like:

pax> awk '
...>   $3=="<>" {
...>     print "Time: "($4-$2)" Seconds";
...>     print "Frames: "$6"";
...>     print "Bytes: "$8"\n"
...>   }' inFile

Time: 1.234 Seconds
Frames: 10
Bytes: 1234

Time: 1.484 Seconds
Frames: 20
Bytes: 9876

Time: 0.424 Seconds
Frames: 42
Bytes: 42

Time: 1.858 Seconds
Frames: 99
Bytes: 97

Upvotes: 1

Related Questions