Home > Mobile >  Find if a substring exists in file through regex in terminal
Find if a substring exists in file through regex in terminal

Time:06-03

I have a build log file. I need to find if build failed or not .The log will have only "Build exited with errorcode 0" if it is passed. Now what I am looking for in the log file is regex similar to this "Build exited with errorcode *", where * could be any return value except zero. If substring is found , the build has failed

For example:

  1. If "Build exited with errorcode 0" is found then build passed.
  2. If both "Build exited with errorcode 0", "Build exited with errorcode 1" , then build failed

It could be really done in python or other language but I am looking to do it terminal through regex.

CodePudding user response:

if grep -Eq 'Build exited with errorcode ([^0]|[^[:space:]]{2,})' logfile; then
    echo build failed
else
    echo build passed
fi

This will print build failed, if the log file contains any instance of the above string, where the word after errorcode is anything other than a single zero. If you want build failed for anything other than one or more zeroes, you could instead use: grep -q 'Build exited with errorcode [^[:space:]]*[^0][^[:space:]]*'.

Note that in either case, even a period in the logfile (Build exited with errorcode 0.) would print 'failed'.

In other words, all instances of this string must be Build exited with errorcode 0 for passed (or up to multiple zeroes for the second example, like 000 or 0).

CodePudding user response:

document content:

mm,bn,hello,ssdenl.
sdfsdf,dfdf,dfdfe,ggg.
sdfd,gfgf
dfdfgfgdfgsdfgsdfg
dfgfdg,sdfsdfdhello.sdfasdfas/
sdfsdfdfgdfgdsfhellosdfasdfasdf
sdfasdfdfgffghjdkfjglfdg

Look for the "hello" field

^(?!.*hello).*$
  • Related