Gary Kline wrote:
On Mon, Dec 22, 2008 at 08:53:36AM +0000, Matthew Seaman wrote:Gary Kline wrote:anyway, this is one for giiorgos, or another perl wiz. i've been using the perl subsitution cmd one-liner for years with unfailing success. is there a way of deleting lines with perl using the same idea as: perl -pi.bak -e 's/OLDSTRING/NEWSTRING/g' file1 file2 fileNTo delete lines matching a R.E. (grep -v effectively): perl -ni.bak -e 'm/SOMETHING/ || print;' file1 file2 fileNMatthew,I've been trying, unsuccessfully, to parse the above. What does the "m" [in 'm/SOMETHING/' do. i thought it was 'match' ... and another one, just FWIW: can perl's regex be set to ignore cases?
It's the standard perl regular expression matching operator. See the section on "Regexp Quote-Like Operators" in the perlop(1) man page. What is does is quite similar in English to the way it's written in perl: it checks each line, and either matches SOMETHING or prints the line. Perl RE's can do just about anything (including some things that are actually impossible with pure Regular Expressions (like counting opening and closing brackets)). To make the match case insensitive just use: m/SOMETHING/i (There are several other ways to achieve the same effect: this is perl, after all)
To delete lines by number from many files -- eg. exclude lines 3 to 7: perl -ni.bak -e 'print unless ( 3 .. 7 ); close ARGV if eof;' \ file1 file2 fileN The malarkey with 'close ARGV' is necessary because otherwise perl won't reset the input line number counter ($.) for each new file.yeah, it's pretty important to reset the counter to zero since i've got so many files. one way to avoid that extended line would be to embed the perl string within a /bin/shell script, :-) Scripts within scripts, eh? lol. Oh: a final question. does the perl regex match vi's /\<foo\> ? it seemed like this plot in /OLDSTRING/ failed last sunday. i'm not entirely sure, tho.
No need to do that. perl is not just for one-liners, and it can be used as the interpreter on shebang lines. In fact, that's probably the most common way of running perl stuff. You can write the above (in a more long-winded way) as: #!/usr/bin/perl -n print unless ( 3 .. 7 ); close ARGV if eof; Save as a file 'foo.pl', make it executable by "chmod +x foo.pl" and then just run it as: ./foo.pl file1 file2 fileNI'm not sure what \<foo\> means in vi(1), but I'll hazard a guess that you're trying to match word boundaries. Sure perl can do that. You want the '\b'
thingy[*], like so: m/\bfoo\b/ which will match 'foo' as a separate word. See perlre(1) for the gory details (but be warned, it's a long read). I did see another answer to your question by Jonathan McKeown who was actually a lot more thorough about how to do this than I was. He discussed the important point about what happens if eg. you apply the 'delete lines 3 .. 7' command to a file with only 6 lines, and more importantly how to stop that ruining your whole day. Cheers, Matthew
thanks much, gary
The range expression ( N .. M ) can take matching terms rather than line numbers, so you can also do things like: perl -ni.bak -e 'print unless ( m/FIRST/ .. m/SECOND/ )' \ file1 file2 fileN Cheers, Matthew -- Dr Matthew J Seaman MA, D.Phil. 7 Priory Courtyard Flat 3 PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate Kent, CT11 9PW
[*] Described as a 'zero width assertion' in the man page, but that's too much typing. -- Dr Matthew J Seaman MA, D.Phil. 7 Priory Courtyard Flat 3 PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate Kent, CT11 9PW
signature.asc
Description: OpenPGP digital signature