pesquisa grep + próxima linha

pesquisa grep + próxima linha

Com o grepcomando, encontrei o texto que preciso da seguinte forma:

grep 'C02' ~/temp/log.txt

Agora, onde quer que eu encontre a string desejada, gostaria de imprimir a linha após a string encontrada.

Por exemplo, digamos que o texto desejado seja abc, e abcse encontre na linha 12, gostaria de imprimir a linha 13 também.

Responder1

Se você estiver usando o sistema Linux, você pode tentar:

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.

Você também pode usar o awk como:

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

informação relacionada