raju Patil raju Patil - 11 months ago 75
Bash Question

Check for string in a file, extract variables and write into another file

I will be getting two or more files with random filenames starting from "createxxxx.out"

Any of these two files can contain a string called Fail.

I have to search for that "Fail" string and Fetch two variables(Variable1 , Variable2) present in the same file (file which contains String 'Fail') and print those two variables in some another file(here i am trying to print it in Error.log file).

I have tried the below script, its not working:

if grep -q "Fail" create_*;


grep 'Variable1\|Variable2' create_* > Error.log
echo -e "Creation Failed"

Answer Source
#use 'egrep' for multiple keyword search
#this script will search for fail/Fail/fAiL words
#from all files starts with 'create'
#and save in fileanme_error.log if fail/variable(s) found
# 'egrep -i' - option also can be used for variable search
# with ignoring case sensitivity

for file in $(ls -1 create_*)

 grep -qi 'fail' $file

 if [ $? -eq 0 ];then

   egrep 'Variable1|Variable2' $file  > $file_Error.log
   echo -e "Creation Failed for file: $file"