[英]grep issue in Perl
下面是來自Perl腳本的..
$search2= join(' ', $mo,$day,$hr); # Output "Aug 2 03"
$LOG1 = "/tmp/log" # Log file path. It has $search2 inside it
$search1 = "issue|error"
這是我遇到問題的那條線...
$failed = `"/bin/egrep" "$search1" "$LOG1"| grep "$search2"`;
沒有grep "search2"
進入下面像輸出
Aug 1 01:03:40 something sudo: pam_unix(sudo:auth): authentication failure; logname=something uid=0 euid=0 tty=/dev/pts/1 ruser= something= user=something
Aug 1 03:34:20 something sudo: pam_unix(sudo:auth): authentication failure; logname=something uid=0 euid=0 tty=/dev/pts/0 ruser= something= user=something
Aug 2 00:43:29 something sudo: pam_unix(sudo:auth): authentication failure; logname=something uid=0 euid=0 tty=/dev/pts/1 ruser= something= user=something
Aug 2 03:02:33 someting sudo: pam_unix(sudo:auth): authentication failure; logname=something uid=0 euid=0 tty=/dev/pts/1 ruser= something= user=something
但是當使用| grep "$search2"
| grep "$search2"
什么也沒得到
print "$failed"; # It is not throwing any output of 'Aug 2 03'
但是,當我在Linux命令行上執行相同的操作時,我會得到所需的輸出。 以下是我在linux命令行上使用的
/bin/egrep "issue|error" "/tmp/log" | grep "Aug 2 03"
請幫助修復它。
我認為您應該遵循Dada在其評論中提供的建議。 如果您發布的內容確實是您所看到的,則此操作將無效:
您可以像這樣建立字符串:
$search2= join(' ', $mo,$day,$hr);
這意味着您在月份和日期之間只有一個空格。 此外,您復制了不使用搜索字符串而獲得的輸出,如下所示:
Aug 1 01:03:40 something sudo: pam_unix(sudo:auth): authentication failure; logname=something uid=0 euid=0 tty=/dev/pts/1 ruser= something= user=something
顯然,這不是8月到1日之間的單個空格。
我非常確定,如果使用十六進制編輯器查看文件/tmp/log
,則不會在“ Aug”和“ 1”之間看到單個空格。
您也可以從命令行調用命令並將其通過管道傳遞給xxd
,以查看十六進制內容。
您無需為此呼叫grep:
use autodie;
open my $fh, "<", $LOG1;
my $failed = "";
while (<$fh>) {
$failed .= $_ if /$search1/o && /$search2/o;
}
close $fh;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.