grep-Suche + nächste Zeile

grep-Suche + nächste Zeile

Mit dem grepfolgenden Befehl habe ich den benötigten Text gefunden:

grep 'C02' ~/temp/log.txt

Jetzt möchte ich überall dort, wo ich die gewünschte Zeichenfolge finde, die Zeile nach der gefundenen Zeichenfolge ausdrucken.

Angenommen, der gewünschte Text lautet abcund abcbefindet sich in Zeile 12. Dann möchte ich auch Zeile 13 drucken.

Antwort1

Wenn Sie ein Linux-System verwenden, können Sie Folgendes versuchen:

grep -A1 "C02" ~/temp/log.txt


OPTIONS
       -A NUM, --after-context=NUM
              Print NUM lines of trailing context after matching lines.  Places a line containing -- between contiguous groups of matches.
       -B NUM, --before-context=NUM
              Print NUM lines of leading context before matching lines.  Places a line containing -- between contiguous groups of matches.
       -C NUM, --context=NUM
              Print NUM lines of output context.  Places a line containing -- between contiguous groups of matches.

Sie können awk auch wie folgt verwenden:

awk '/C02/{print;getline;print}' ~/temp/log.txt

verwandte Informationen