nawk/awk: How to present an error message while there is no string match? -


i'll start saying forum excellent source of knowledge. need in presenting no match error message.

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

i'm looking 'only' string , if there match, presented

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"}' 

output: only

if i'm looking 'only' string , checking if there apple on line, i'll no match message

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"}' 

output: no match

but if i'm looking 'apple' string (there no such) , check if it's there, don't 'no match' message.

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"}' 

the output blank. how can change behaviour, want 'no match' message.

in awk.

if searchphrase isn't found @ all.

awk '/searchphrase/{c++} end{if(c == 0) print "no match"}' file 

if want print no match every line without match.

awk '!/searchphrase/{print "no match"}' file 

if want still print searchphrase when found

awk '/searchphrase/{c++; print} end{if(c == 0) print "no match"}' file 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -