开发者

Perl regex: remove everything (including line breaks) until a match is found

Apologies for the simple question. I don't clean text开发者_如何学运维 or use regex often.

I have a large number of text files in which I want to remove every line until my regex finds a match. There's usually about 15 lines of fluff before I find a match. I was hoping for a perl one-liner that would look like this:

perl -p -i -e "s/.*By.unanimous.vote//g" *.txt

But this doesn't work.

Thanks


Solution using the flip-flop operator:

perl -pi -e '$_="" unless /By.unanimous.vote/ .. 1' input-files

Shorter solution that also uses the x=!! pseudo operator:

per -pi -e '$_ x=!! (/By.unanimous.vote/ .. 1)' input-files


Have a try with:

If you want to get rid until the last By.unanimous.vote

perl -00 -pe "s/.*By.unanimous.vote//s" inputfile > outputfile

If you want to get rid until the first By.unanimous.vote

perl -00 -pe "s/.*?By.unanimous.vote//s" inputfile > outputfile


Try something like:

perl -pi -e "$a=1 if !$a && /By\.unanimous\.vote/i; s/.*//s if !$a" *.txt

Should remove the lines before the matched line. If you want to remove the matching line also you can do something like:

perl -pi -e "$a=1 if !$a && s/.*By\.unanimous\.vote.*//is; s/.*//s if !$a" *.txt

Shorter versions:

perl -pi -e "$a++if/By\.unanimous\.vote/i;$a||s/.*//s" *.txt
perl -pi -e "$a++if s/.*By\.unanimous\.vote.*//si;$a||s/.*//s" *.txt


You haven't said whether you want to keep the By.unanimous.vote part, but it sounds to me like you want:

s/[\s\S]*?(?=By\.unanimous\.vote)//

Note the missing g flag and the lazy *? quantifier, because you want to stop matching once you hit that string. This should preserve By.unanimous.vote and everything after it. The [\s\S] matches newlines. In Perl, you can also do this with:

s/.*?(?=By\.unanimous\.vote)//s


Solution using awk

awk '/.*By.unanimous.vote/{a=1} a==1{print}' input > output
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜