PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > How do I print only the first grep match?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

How do I print only the first grep match?

Réponse
 
LinkBack Outils de la discussion
Vieux 06/12/2006, 20h19   #1
R Krause
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How do I print only the first grep match?

How do I search a compressed log file for a given regexp pattern, and
print only the first line of results?

At first I considered piping "grep -Z" to "head -1". But grep still has
to process the entire file for all matches. When only one is required,
that doesn't seem to be very efficient.

My second thought was to use gawk since it could do a pattern match and
print only the first matching line. It's more elegant for this purpose,
but it doesn't decompress files (to my knowledge), so I'd have to
resort to piping the results of "zcat" (that that the entire
uncompressed file) to "gawk", which is also quite inefficient.

It would be nice if grep had an option to only print N results, but
such a feature doesn't seem to be implemented. Am I missing something?
Is it maybe possible that "head -1" actually terminates after printing
the first line, thus forcing "grep -Z" to receive a SIGPIPE and quit
immediately? This has me quite curious.

TIA,
--Randall

  Réponse avec citation
Vieux 06/12/2006, 20h34   #2
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How do I print only the first grep match?

2006-12-6, 12:19(-08), R Krause:
> How do I search a compressed log file for a given regexp pattern, and
> print only the first line of results?
>
> At first I considered piping "grep -Z" to "head -1". But grep still has
> to process the entire file for all matches. When only one is required,
> that doesn't seem to be very efficient.


Not necessarily the whole file. It will probably stop at the
second occurrence (as it will get a SIGPIPE for writing to its
stdout that is now gone) unless it buffers its output in which
case he will stop when he writes its second buffer of output.

>
> My second thought was to use gawk since it could do a pattern match and
> print only the first matching line. It's more elegant for this purpose,
> but it doesn't decompress files (to my knowledge), so I'd have to
> resort to piping the results of "zcat" (that that the entire
> uncompressed file) to "gawk", which is also quite inefficient.


zcat would uncompress chunks at a time. grep -Z for me is to
append a '\0' to a filename instead of a '\n' in grep -l. But if
it's for uncompressing for you, then I'd bet grep calls
uncompress internally.

In

uncompress < file.Z | awk '/pattern/ {print; exit}'
uncompress < file.Z | sed '/pattern/!d;q'

uncompress will again probably write a buffer full worth of data
at a time. Once awk exits, uncompress will terminate (killed by
a SIGPIPE) the next time it tries to write to the pipe.

> It would be nice if grep had an option to only print N results, but
> such a feature doesn't seem to be implemented. Am I missing something?
> Is it maybe possible that "head -1" actually terminates after printing
> the first line, thus forcing "grep -Z" to receive a SIGPIPE and quit
> immediately? This has me quite curious.

[...]

That's what head does. head will stop after it has read one line
from the pipe (it may read more than one line if there's more
than one available though), the problem is that grep will wait
until it has enough to send its output to head (you don't notice
it when you run grep without | head because grep's behavior (wrt
buffering) is different when its stdout is a terminal).

GNU grep has a --line-buffered option to not buffer.

But GNU grep also has the -m <n> option you were after.
-m, --max-count=NUM stop after NUM matches
--line-buffered flush output on every line

So


--
Stéphane
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h05.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,08192 seconds with 10 queries