How to test if a string exists in a HTML file - Unix Script
bash, command-line, shell, string, unix
Solution
Problem is in your use of:
grep -x
Since `grep` command with `-x` attempts to match exact full line. As per `man grep`:
-x, --line-regexp
Only input lines selected against an entire fixed string or regular expression are
considered to be matching lines.
Just use `grep -Fq` instead of `grep -xq`.
PS: It is not recommended to use output from `ls` like this. Better use globbing directly in your `for` loop like this:
for file in *.html; do
echo "processing $file"
done
Problem
I am writing a ksh script. I have a pair of html files in a directory and I need to check whether or not the files contain one of two strings (the strings are mutually exclusive). I then rename the files based on which of the two strings they contain. When testing I was able to use the following code on `.txt` files, but the functionality no longer works when testing for the strings in `.html` files: ``` outageString='Scheduled Outage List' jobString='Scheduled Job List' for file in `ls -1t $fileNameFormat | head -n 2` do if grep -xq "$outageString" "$file"; then mv "$file" "$outageFileName" elif grep -xq "$jobString" "$file"; then mv "$file" "$jobFileName" fi done ``` Note: I have tested the `ls` command above independently and it returns the appropriate files. File Content: ``` <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title> OUS: Scheduled Outage List </title> </head> <body> <h3> OUS: Scheduled Outage List </h3> . . . ``` Q: Does anyone have any insight as to why `grep` is not returning the appropriate value when searching for the strings in the two files(i.e., why `grep` does not recognize that the string exists in the file)? Similar Question: How to test if string exists in file with Bash shell?