2013-04-14 88 views
0

需要對這個 這裏我有什麼和想做 一些幫助這是我的文件,稱爲file.txt的awk來分析文件

Sequences times  Ip1  ip2  Protocol  info 
1  2 10.18.19.12 12.29.29.18 udp  hsahdsdd 
2  4 10.13.15.67 12.28.29.19 tcp  blabla 
3  7 10.12.27.28 12.17.281.19 udp  hkkkljkl 

我想(用awk)

  • 計數有多少行已經和打印這些信息+都在叫protocol.tx
  • 一個輸出文件中找到的協議名稱計數IP1的線,並添加所有IP的清單當然在名爲ip.txt
  • 另一個輸出文件

感謝 這裏是我嘗試

#!/usr/bin/perl 
$fname = $ARGV[0]; 
open(FILE, $fname) || die ("cant read \n"); 
while($ligne=<FILE>) 
{ 
chop ($ligne); 
my ($No, $time, $Ip1, $Ip2, $Proto, $info) = split (/ /, $ligne_); 
} 
system("awk '{print \$6}' $fname"); 
system ("awk 'END {print NR}' $fname") >line.txt; 
+3

呵呵?你爲什麼要使用'awk'(從Perl程序中)來做到這一點? – ikegami

+1

對於換行符,最好使用'chomp'而不是'chop'。 – squiguy

回答

3

system這形式需要一個shell命令,所以

use autodie qw(system); 
use String::ShellQuote qw(shell_quote); 

system(shell_quote('awk', 'END {print NR}', $fname) . ">line.txt"); 

,但它是愚蠢的使用awk從Perl中做到這一點很簡單的工作。