BASH: determine which of three strings is in a file -
i need determine of 3 strings found in file. guaranteed 1 of 3 found in file. want different thing based on of 3 in file.
i trying do:
myfile="/home/directory/file.xml" case "stringone" in *$myfile*) #do thing ;; esac case "stringtwo" in *$myfile*) #do thing b ;; esac case "stringthree" in *$myfile*) #do thing c ;; esac
however, not working, , program gets stuck. there better way, or quick way fix way?
since file guaranteed contain 1 of them, can find 1 , use case
statements:
myfile="/home/directory/file.xml" str=$(grep -o -m 1 -e 'stringone|stringtwo|stringthree' $myfile) [[ -z ${str} ]] && { echo "no match found!"; exit 1; } case "${str}" in stringone) #do thing ;; stringtwo) #do thing b ;; stringthree) #do thing c ;; esac
grep
options:
-o
print matching word. know word there in input file.-m 1
ensure stops @ first match. doesn't need scan rest of file.-e
regex match match match either 1 of 3 strings.
Comments
Post a Comment