开发者

Perform a regex operation over files in directory

I want to replace instances of <span class='i'> </span> with <i> </i> because I decided I want to format my pages this way instead. So I have come up with this command:

perl -pe "s/<span +class *= *['\"]i['\"] *>(.*?)<\/span>/<i>\1<\/i>/g"

I could make it开发者_开发问答 more elaborate but I really don't think there are instances of weirdly formed tags like < / span> or anything so I'll leave it at that. It does have a non greedy capture which is why I used perl -p rather than sed.

So this will output the correctly modified lines but I'm not sure about the best way to send multiple files through this command. What's the best way to do it if I want all of pages/*.html to have the span class='i' tags fixed? Does bash provide some provision for doing this other than a for loop?


@Steven, as per your comment to the answer by @SiegeX, the following will work fine:

perl -pi -e "s/<span +class *= *['\"]i['\"] *>(.*?)<\/span>/<i>\1<\/i>/g" *.html

I would have Perl create backups of the files though, so change the first part to

perl -pi.bak -e ...


The following will iterate over all html files in pages/ and do an in-place edit with your perl script .

#!/bin/bash

for file in pages/*.html; do
  perl -pi -e "s/<span +class *= *['\"]i['\"] *>(.*?)<\/span>/<i>\1<\/i>/g" "$file"
done
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜