I want to grep through all my HTML files and see if I have any bad tags there, example <br> , <hr> and so on, that is the I want to see what tags are not closed in the HTML file.

for htmlFile in `ls -f *.html ` ; do
        if `cat $htmlFile | grep -inE "\<br\>"` ; then
           echo "In file $htmlFile there are errors on the following lines: "  >> ~/Desktop/$1_errors.txt
           cat $htmlFile | grep -in "<br>"| cut -d ":" -f1 >> ~/Desktop/$1_errors.txt
           echo "----------------------------------------" >> ~/Desktop/$1_errors.txt

But I get an error, I am suspect its my regex.

./script.sh: line 14: 10:<BR: command not found

That is one of the error I get :)

  • 11
    Uh-oh: stackoverflow.com/questions/1732348/… – goldilocks Jun 11 '14 at 13:13
  • 1
    See also this site for why you shouldn't be parsing the output of ls in this case. – Caleb Jun 11 '14 at 13:27
  • I have tried using shopt -s nullglob but I dont know how to undo shopt – gkmohit Jun 11 '14 at 13:34
  • 5
    1) Don't use regex to parse HTML 2) Never use for loops with ls (and avoid parsing ls in general) 3) Don't cat file | grep, just grep file. – terdon Jun 11 '14 at 13:39
  • 1
    Have a look at the link given by goldilocks in the 1st comment. Also, just think about it. Consider the differences between XHTML and HTML. For example, </br> is not needed in the latter and there are various other tags that don't need closing tags. How would you deal with nested tags? The whole thing is just way too complex for regular expressions. – terdon Jun 11 '14 at 13:53

Your problem(s) are in this line:

if `cat $htmlFile | grep -inE "\<br\>"` ; then

It's telling the shell to:

  • cat a file,
  • parse it and look for lines that match the <br> tag,
  • execute the output

The problem is the last step, you shouldn't execute the output of the command but test it:

if grep -inEq "\<br\>" $htmlFile ; then

Of course, to parse HTML you should use a real parser, no regexes.


Even if you fix the errors in your shell syntax as Braiam's answer correctly identifies, you will never get this to really work right. At best you might find a few mistakes of the sort you know you make; but you will never get grep to reliably find broken tags of the sort you might not have already noticed you are breaking.

To do this right you need to use something that actually parses and understands HTML tags. There are lots of HTML validation tools out there that can point out where you have broken tag syntax. One of the most common that you should start by checking out is tidy. In particular try running it on your files with this flag:

 -errors, -e         show only errors and warnings

This will point out where you have broken tags such as things that are not closed the way they should be to comply with your doctype.

There are also lots of other options besides tidy for doing syntax checking on HTML. Using the W3C validator is a good one. Whatever you do—at the risk of the computer gods raining fire and brimstone on you—don't even try to parse html syntax with regular expressions like grep uses for pattern matching.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.