Sydowh Sydowh - 2 months ago 14
Bash Question

Search on HTM Files and add at first line

I can't find the good way to search which HTM* files doesnt have DOCTYPE and add this DOCTYPE to this specific file

I got the list of file with this :

for i in ` find . -name "*.htm*" -print`; do grep -L "DOCTYPE" $i;done;


But I can't find how to work on this list with sed

I tried :

for i in ` find . -name "*.htm*" -print; grep -L "DOCTYPE"`; do sed -i '1i <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">' $i; done;


But it's adding the line on all HTM* files

Thanks

Answer

You should do this using the -exec option to find, rather than writing your own loop:

find . -name '*.htm?' -exec sed -i .bak '1 { /DOCTYPE/ ! i\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
}' {} +

This finds all files ending in .htm followed by an optional extra character and executes the sed command. If /DOCTYPE/ isn't matched on the first line, the string is inserted.

Using -exec with {} + means that the minimum number of instances of sed are used rather than running a separate instance per file found.

I added an argument after -i so that backup files are created with the suffix .bak.