开发者

Adding characters to the beginning and end of a line starting with a specific string

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.


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?

for f in *.htm*; do
  mv "$f" "$f.tmp"
  sed 's#^\(.*common/javascript/jquery/jquery.tools-1.2.4.min.js.*\)#<!-- \1 -->#' "$f.tmp" > "$f"
  rm "$f.tmp" ;
done
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜