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
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.