将字符添加到以特定字符串开头的行的开头和结尾
我有很多 html 文件,需要注释掉特定的 JavaScript 行:
<script src="/common/javascript/jquery/jquery.tools-1.2.4.min.js" type="text/javascript"></script>
我想通过命令行执行的操作是在目录中搜索 .htm 文件中的字符串:“/common/javascript/jquery/jquery .tools-1.2.4.min.js”并将 到行尾。
有些文件包含 type=
而有些则不包含,这就是为什么我想使用 src 值进行搜索并添加到行的开头和结尾。
感谢您的帮助。我很感激。
I have quite a few html files where I need to comment out a specific line of JavaScript:
<script src="/common/javascript/jquery/jquery.tools-1.2.4.min.js" type="text/javascript"></script>
What I would like to do via command line, is search .htm files in the directory for the string: "/common/javascript/jquery/jquery.tools-1.2.4.min.js" and add <!--
to the beginning of the respective line containing the string, and-->
to the end of the line.
Some files include type=
and some do not, which is why I'd like to search using the src value and add to the beginning and end of line.
Thanks for the help. I appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您将“整行”替换为要注释掉的整个 js 行,这将输出修改后的文件。
sed 's/\(整行\)/</' file.htm
现在只需迭代目录中的所有文件
for f in *htm;做
sed 's/\(整行\)/</' $f > $f.new
完成
我将让您弄清楚如何将它们移回到正确的文件名。 (也许是一个新目录?也许是一个
mv
命令?无论您的情况如何最好。)This will output a modified file if you replace "that whole line" with that entire js line you want to comment out.
sed 's/\(that whole line\)/<<!--\1-->/' file.htm
Now just iterate that over all the files in the dir
for f in *htm; do
sed 's/\(that whole line\)/<<!--\1-->/' $f > $f.new
done
I'll let you figure out how to handle moving them back to the right filenames. (Maybe a new dir? Maybe a
mv
command? Whatever's best in your situation.)这是你想要的吗?
Is this what you want?