Condition where you find the name "test" I print OK if NO

Asked

Viewed 80 times

1

this is the code I’m trying to solve and its result!

awk '{if ($0 ~ "teste") { print "ok"} else {print "no" }}' arquivo.txt

no
ok
no
no
no

as you can see he checked every line himself had the name test and printed okay and on lines that had not printed in the !!

I know awk works line by line!

wanted him to just tell me OK if you have the expression I seek or IN THE if there is no!

1 answer

0


In awk:

awk '/teste/ {visto=1; exit} END {print visto ? "OK" : "NO"}' arquivo.txt

Explanation:

  • /teste/ { cosas } when Awk finds the text "test", it executes the code in {}.
  • visto=1; exit} in this case, defines a boolean variable visto.
  • END {print visto ? "OK" : "NO"} when it ends, aim if the variable is 1 o no. If it is 1, print "OK"; otherwise print "NO".

Mai I prefer Bash:

if grep -q "teste" arquivo.txt; then
   echo "OK"
else
   echo "NO"
fi

In a line:

if grep -q teste archivo.txt; then echo "OK"; else echo "NO"; fi
  • thank you so much bro! that’s what I wanted! with bash I knew how to make more wanted just with awk! more pod explain to me how it works not intendi some parts!

  • @socketplus of course! Updated

  • very obg! @fedorqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.