如何使用perl在文件中找到模式$ iperf

时间:2009-06-30 06:12:53

标签: regex perl

如何在文件中找到并显示单词$ iperf

该文件将如下所示

$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 个答案:

答案 0 :(得分:1)

听起来你需要一个正则表达式。您的数据中不存在字符串'$iperf',因此我假设您的意思是'iperf'。您可以通过一次一行地循环文件并使用正则表达式测试每一行来查找包含该字符串的行。如果正则表达式成功,那么您可以打印该行。

#!/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)
------------------------------------------------------------