Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 23 Dec 2008 20:27:53 +0000
From:      Matthew Seaman <m.seaman@infracaninophile.co.uk>
To:        Gary Kline <kline@thought.org>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Sed question
Message-ID:  <495149C9.4050705@infracaninophile.co.uk>
In-Reply-To: <20081223190849.GD98784@thought.org>
References:  <20081221053407.GA87868@thought.org> <877i5unkx4.fsf@kobe.laptop> <1229854084.6392.52.camel@ethos> <20081221140658.GA24691@marge.bs.l> <20081221222744.GA28185@thought.org> <494F5590.3090400@infracaninophile.co.uk> <20081223190849.GD98784@thought.org>

next in thread | previous in thread | raw e-mail | index | archive | help
This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
--------------enig54719555333A7AA1F1D8B2B8
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: quoted-printable

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 fileN
>> To delete lines matching a R.E. (grep -v effectively):
>>
>>    perl -ni.bak -e 'm/SOMETHING/ || print;' file1 file2 fileN
>>
>=20
> 	Matthew,=20
>=20
> 	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, a=
fter
all)

>=20
>> 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.
>=20
>=20
> 	yeah, it's pretty important to reset the counter to zero since
> 	i've got so many files.
>=20
> 	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 comm=
on way
of running perl stuff.  You can write the above (in a more long-winded wa=
y) 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 fileN

I'm not sure what \<foo\> means in vi(1), but I'll hazard a guess that yo=
u're=20
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=
=2E  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 act=
ually a
lot more thorough about how to do this than I was.  He discussed the impo=
rtant
point about what happens if eg. you apply the 'delete lines 3 .. 7' comma=
nd to a
file with only 6 lines, and more importantly how to stop that ruining you=
r whole
day.

	Cheers,

	Matthew


> 	thanks much,
>=20
> 	gary


>=20
>=20
>> 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
>>
>> --=20
>> Dr Matthew J Seaman MA, D.Phil.                   7 Priory Courtyard
>>                                                  Flat 3
>> PGP: http://www.infracaninophile.co.uk/pgpkey     Ramsgate
>>                                                  Kent, CT11 9PW
>>
>=20
>=20
>=20

[*] Described as a 'zero width assertion' in the man page, but that's too=

much typing.

--=20
Dr Matthew J Seaman MA, D.Phil.                   7 Priory Courtyard
                                                  Flat 3
PGP: http://www.infracaninophile.co.uk/pgpkey     Ramsgate
                                                  Kent, CT11 9PW


--------------enig54719555333A7AA1F1D8B2B8
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEAREIAAYFAklRSc4ACgkQ8Mjk52CukIyNqgCfV3aOZU0iWnfSUKzgvrNL9XC5
/GgAnAjLVWRE+oXka1sEIHMCzxHxv2jL
=BHOE
-----END PGP SIGNATURE-----

--------------enig54719555333A7AA1F1D8B2B8--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?495149C9.4050705>