| Date: Thu, 19 Feb 2004 19:48:40 +0000
| From: Padraig Brady <padraig.brady at corvil.com>
|
| OK I've a (very big) file like:
|
| 0
| 0
| 1
| 1
| 2
| 2
| 3
| 3
| :
|
| I would like to find and print the "2" lines,
| AND THEN STOP PRCOCESSING the humungous file
| [ using sed(1) ].
if the above really is what the file is like,
I'd consider something like:
sed -n -e '/3/q' -e '/2/p' | ... processing `2' lines ...
the sed(1) prints all (and only) `2' lines, and
stops when it finds a (_known_) line which first
occurs (soon) after the `2' lines; in this case,
a `3' line.
otherwise:
sed -n -e '
/2/{
p
x
b
}
s/^.*$/anything which does not match the RE/
x
/2/q'
where “the RE‟ is /2/. the key here is to use
the hold space, which starts out empty. as long
as the hold space does not match /2/, then we
either have not seen, or are still processing,
the `2'-lines; as soon as it does match /2/,
then we are done with all the `2'-lines.
the above, of course, can be written more tersely,
something like (NOT tested!):
sed -n -e '/2/{p;x;b;}' -e 's/^.*$/no/' -e x -e /2/q
or
sed -n -e '/2/{p;x;b;};s/^.*$/no/;x;/2/q'
you can probably replace the `b' with `d'; and/or
omit the `s/^.*$/no/' altogether; and I am sure
other tweaks are possible .....
it is equally trivial in awk(1) --- which _may_
not be as slow as you think! --- but sed(1) is
probably faster if a tiny amount more obscure.
cheers!
-blf-
--
«How many surrealists does it take to | Brian Foster Montpellier,
change a lightbulb? Three. One calms | blf at utvinternet.ie France
the warthog, and two fill the bathtub | Stop E$$o (ExxonMobile)!
with brightly-colored machine tools.» | http://www.stopesso.com
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!