开发者

Regular expression and sed to remove all occurences of some string from hundreds of files

开发者 https://www.devze.com 2023-03-01 22:50 出处:网络
I\'m perusing the web for information about regular expressions and sed usage.I\'ve also got sed\'s manual open.Still, I\'m posting this question here because I\'m sure someone uses the two often enou

I'm perusing the web for information about regular expressions and sed usage. I've also got sed's manual open. Still, I'm posting this question here because I'm sure someone uses the two often enough that they can probably answer this question before I work out a 开发者_运维问答solution.

I've got a few hundred html documents with links like the following:

http://www.example.com/subfolder/abc.asp?page=1#main

I need to remove the "#main"

Does a pattern pop into mind?


Try this sed:

sed 's/^\(.*\)#.*$/\1/'

Or this better sed command:

sed 's/#.*$//'


Here's a snippet that works with perl on the command line. It's not sed, but I had it on hand:

perl -i -pe 's/#main//' *.html

To run it, and have it make backups, you can use:

perl -pi.bak -e 's/#main//' *.html


Assuming that #main is specific enough to do a simple find and replace:

find . -name '*.html' -print0 | xargs -0 sed -i 's/#main//g'
0

精彩评论

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