From owner-freebsd-questions@FreeBSD.ORG Tue Dec 23 20:28:05 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id F3BCD1065670 for ; Tue, 23 Dec 2008 20:28:04 +0000 (UTC) (envelope-from m.seaman@infracaninophile.co.uk) Received: from smtp.infracaninophile.co.uk (gate6.infracaninophile.co.uk [IPv6:2001:8b0:151:1::1]) by mx1.freebsd.org (Postfix) with ESMTP id 3DD208FC17 for ; Tue, 23 Dec 2008 20:28:04 +0000 (UTC) (envelope-from m.seaman@infracaninophile.co.uk) Received: from happy-idiot-talk.infracaninophile.co.uk (localhost [IPv6:::1]) (authenticated bits=0) by smtp.infracaninophile.co.uk (8.14.3/8.14.3) with ESMTP id mBNKRwXf004666; Tue, 23 Dec 2008 20:27:59 GMT (envelope-from m.seaman@infracaninophile.co.uk) X-DKIM: Sendmail DKIM Filter v2.7.2 smtp.infracaninophile.co.uk mBNKRwXf004666 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=infracaninophile.co.uk; s=200708; t=1230064079; bh=2DHqeojr37AQQT refpw87anTBn9GUPGAhmSaYxFnJ9o=; h=Message-ID:Date:From:MIME-Version: To:CC:Subject:References:In-Reply-To:Content-Type:Cc:Content-Type: Date:From:In-Reply-To:Message-ID:Mime-Version:References:To; z=Mes sage-ID:=20<495149C9.4050705@infracaninophile.co.uk>|Date:=20Tue,=2 023=20Dec=202008=2020:27:53=20+0000|From:=20Matthew=20Seaman=20|Organization:=20Infracaninophile|User -Agent:=20Thunderbird=202.0.0.18=20(X11/20081125)|MIME-Version:=201 .0|To:=20Gary=20Kline=20|CC:=20freebsd-questions @freebsd.org|Subject:=20Re:=20Sed=20question|References:=20<2008122 1053407.GA87868@thought.org>=20<877i5unkx4.fsf@kobe.laptop>=20<1229 854084.6392.52.camel@ethos>=20<20081221140658.GA24691@marge.bs.l>=2 0<20081221222744.GA28185@thought.org>=20<494F5590.3090400@infracani nophile.co.uk>=20<20081223190849.GD98784@thought.org>|In-Reply-To:= 20<20081223190849.GD98784@thought.org>|X-Enigmail-Version:=200.95.6 |Content-Type:=20multipart/signed=3B=20micalg=3Dpgp-sha256=3B=0D=0A =20protocol=3D"application/pgp-signature"=3B=0D=0A=20boundary=3D"-- ----------enig54719555333A7AA1F1D8B2B8"; b=G5iLSddWKNGAzGJOiBasTWVH foAINvsnHys/mDWPM4cQ0EEtSXFKFijei7TtN5RtQw2zPCiDwIGVdKspV1+ytnONtFU zWYfFEuzCRK7URIlMmuscpKy5AMjQPtJZ+zglQmHgYZfv29G5wFGZt3GrFoAv8ufoUQ agmV47gO3rNKY= Message-ID: <495149C9.4050705@infracaninophile.co.uk> Date: Tue, 23 Dec 2008 20:27:53 +0000 From: Matthew Seaman Organization: Infracaninophile User-Agent: Thunderbird 2.0.0.18 (X11/20081125) MIME-Version: 1.0 To: Gary Kline 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> In-Reply-To: <20081223190849.GD98784@thought.org> X-Enigmail-Version: 0.95.6 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="------------enig54719555333A7AA1F1D8B2B8" X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.0.1 (smtp.infracaninophile.co.uk [IPv6:::1]); Tue, 23 Dec 2008 20:27:59 +0000 (GMT) X-Virus-Scanned: ClamAV 0.94.2/8795/Tue Dec 23 17:58:21 2008 on happy-idiot-talk.infracaninophile.co.uk X-Virus-Status: Clean X-Spam-Status: No, score=-3.1 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VERIFIED,NO_RELAYS autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on happy-idiot-talk.infracaninophile.co.uk Cc: freebsd-questions@freebsd.org Subject: Re: Sed question X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 23 Dec 2008 20:28:05 -0000 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 > /\ ? 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 \ 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--