tags:

views:

352

answers:

1

How can I find and display the word $iperf in a file

The file will look like this

$iperf -c 172.29.38.67 -m -M 64 -i 5 -t 20 -P 10
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
------------------------------------------------------------

~ $ iperf -c 172.29.38.67 -m -M 128 -i 5 -t 20 -P 10
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
------------------------------------------------------------
Client connecting to 172.29.38.67, TCP port 5001
TCP window size: 16.0 KByte (default)
------------------------------------------------------------
+1  A: 

It sounds like you need a regex. The string '$iperf' does not exist in your data, so I am going to assume you mean 'iperf'. You can find the lines that contain that string by looping over the file one line at a time and testing each line with a regex. If the regex succeeds, then you can print the line.

#!/usr/bin/perl

use strict;
use warnings;

while (<DATA>) {
    print if /\biperf\b/;
}

__DATA__
$iperf -c 172.29.38.67 -m -M 64 -i 5 -t 20 -P 10
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
WARNING: attempt to set TCP maximum segment size to 64, but got 536
------------------------------------------------------------

~ $ iperf -c 172.29.38.67 -m -M 128 -i 5 -t 20 -P 10
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
WARNING: attempt to set TCP maximum segment size to 128, but got 536
------------------------------------------------------------
Client connecting to 172.29.38.67, TCP port 5001
TCP window size: 16.0 KByte (default)
------------------------------------------------------------
Chas. Owens