Come si esegue il grep di un file e si ottengono le 5 righe successive


Risposte:


216

Tu vuoi:

grep -A 5 '19:55' file

Da man grep:

Context Line Control

-A NUM, --after-context=NUM

Print NUM lines of trailing context after matching lines.  
Places a line containing a gup separator (described under --group-separator) 
between contiguous groups of matches.  With the -o or --only-matching
option, this has no effect and a warning is given.

-B NUM, --before-context=NUM

Print NUM lines of leading context before matching lines.  
Places a line containing a group separator (described under --group-separator) 
between contiguous groups of matches.  With the -o or --only-matching
option, this has no effect and a warning is given.

-C NUM, -NUM, --context=NUM

Print NUM lines of output context.  Places a line containing a group separator
(described under --group-separator) between contiguous groups of matches.  
With the -o or --only-matching option,  this  has  no effect and a warning
is given.

--group-separator=SEP

Use SEP as a group separator. By default SEP is double hyphen (--).

--no-group-separator

Use empty string as a group separator.

2
Wow wow wow. Grazie mille.
PhillipMwaniki

Sarebbe fantastico se ci fosse un modo per non limitare l'output a una certa quantità di righe ma stampare tutte le righe dopo quella corrispondente.
Matthias Braun

4

Alcune awkversioni.

awk '/19:55/{c=5} c-->0'
awk '/19:55/{c=5} c && c--'

Quando viene trovato il motivo, impostare c=5
Se cè vero, stampare e diminuire il numero dic


2

Ecco una soluzione sed:

sed '/19:55/{
N
N
N
N
N
s/\n/ /g
}' file.txt
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.