i have a scenario where i have three words in a file called demo.txt
My three words are : apple , mango , grapes
i want to write a one linear command to check if all three word exist in a file then display match found successfully or otherwise display match not found
how to do with awk command in one linear way
below is my code not working
awk '{print (/apple/|/mango/|/grapes/ ? "true : match found " : "false : not found ")}' /D/demo.txt
sample file : demo.txt
abc:apple
b:mango
fgg:grapes
ball , candle
vik,mani
raj,vilas
apart from first file **i have second file which contain below text
Azr
hjkds
$$ABC=%wkde**mo
$Bilas=%ram
xyz
vxbnx
ram
I want to check whether the exact keyword present or not $$ABC=%wkde**mo $Bilas=%ram
if match found display message keyword found or otherwise display keyword not found
Could you please try following. In case your awk supports word boundaries.
awk '
/\<apple\>/{
app_found=1
}
/\<mango\>/{
mango_found=1
}
/\<grapes\>/{
grapes_found=1
}
END{
if(app_found && mango_found && grapes_found){
print "All 3 words found."
}
else{
print "All 3 words are NOT present in whole Input_file."
}
}
' Input_file
Edited answer: the following command has been tested with the input sample provided above and works as desired:
awk '
BEGIN { RS = "§" }
{print (/apple/ && /mango/&&/grapes/) ? "match found" : "match not found"}
' demo.txt
I used the char §
as record separator because there is no such a char in the input and because RS = "\0"
is not portable. If you feel it could happen that such a §
could occur in the input file, you can use the portable solution below:
awk '
{ i = i $0 }
END { print (i ~ /apple/ && i ~ /mango/ && i ~ /grapes/) ? "match found" : "match not found"}
' demo.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With