Wie grepst du eine Datei und erhältst die nächsten 5 Zeilen?


117

Wie greperstelle ich eine Datei 19:55und erhalte die Zeile 1,2,3,4,5?

2013/10/08 19:55:27.471
Line 1
Line 2
Line 3
Line 4
Line 5

2013/10/08 19:55:29.566
Line 1
Line 2
Line 3
Line 4
Line 5

2
Ihr Titel sieht brillant aus. Weißt du - nein, ich grep nicht nach einer Datei und bekomme 5 Zeilen: p
— Alma Do

Antworten:


216

Sie wollen:

grep -A 5 '19:55' file

Von 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. Tausend Dank.
— PhillipMwaniki

Es wäre großartig, wenn es eine Möglichkeit gäbe, die Ausgabe nicht auf eine bestimmte Anzahl von Zeilen zu beschränken, sondern alle Zeilen nach der übereinstimmenden zu drucken .
— Matthias Braun

4

Eine awkVersion.

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

Wenn das Muster gefunden wurde, setzen Sie c=5
If auf ctrue, drucken Sie die Anzahl von und verringern Sie siec


2

Hier ist eine sed Lösung:

sed '/19:55/{
N
N
N
N
N
s/\n/ /g
}' file.txt
Durch die Nutzung unserer Website bestätigen Sie, dass Sie unsere Cookie-Richtlinie und Datenschutzrichtlinie gelesen und verstanden haben.
Licensed under cc by-sa 3.0 with attribution required.