[英]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我找不到搜索哪些 HTM* 文件没有 DOCTYPE 并将此 DOCTYPE 添加到此特定文件的好方法
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但是我找不到如何使用 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但它在所有 HTM* 文件上添加了这一行
Thanks谢谢
You should do this using the -exec
option to find
, rather than writing your own loop:您应该使用
-exec
选项来执行此操作find
,而不是编写自己的循环:
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.这将查找以
.htm
结尾的所有文件,后跟一个可选的额外字符并执行 sed 命令。 If /DOCTYPE/
isn't matched on the first line, the string is inserted.如果
/DOCTYPE/
在第一行不匹配,则插入字符串。
Using -exec
with {} +
means that the minimum number of instances of sed are used rather than running a separate instance per file found.将
-exec
与{} +
意味着使用最少数量的 sed 实例,而不是为每个找到的文件运行单独的实例。
I added an argument after -i
so that backup files are created with the suffix .bak
.我在
-i
之后添加了一个参数,以便创建后缀为.bak
备份文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.