0

我首先要说这个论坛是一个极好的知识来源。我需要您的帮助来显示 No Match 错误消息。

printf "some\nwhere\nonly\nwe\nknow\n" > test.txt 

我正在寻找“唯一”字符串,如果有匹配项,则会显示

nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=0 a=0 s="only" test.txt | awk '{if ($0 ~ /only/) print; else print "No Match"}'

输出:只有

如果我正在寻找 'only' 字符串并检查此行是否还有苹果,我将收到 No match 消息

nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=0 a=0 s="only" test.txt | awk '{if ($0 ~ /apple/) print; else print "No Match"}'

输出:不匹配

但是,如果我正在寻找“苹果”字符串(没有这样的字符串)并检查它是否存在,我不会收到“不匹配”消息。

nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=0 a=0 s="apple" test.txt | awk '{if ($0 ~ /apple/) print; else print "No Match"}'

输出为空白。我怎样才能改变这种行为,我想得到“不匹配”的消息。

4

1 回答 1

0

在 awk。

如果根本找不到搜索短语。

awk '/searchphrase/{c++} END{if(c == 0) print "No Match"}' file

如果您想为没有匹配的每一行打印不匹配。

awk '!/searchphrase/{print "No Match"}' file

如果您想在找到时仍打印搜索短语

awk '/searchphrase/{c++; print} END{if(c == 0) print "No Match"}' file
于 2014-06-26T07:12:34.323 回答