С помощью grep
команды я нашел нужный мне текст следующим образом:
grep 'C02' ~/temp/log.txt
Теперь, где бы я ни находил нужную строку, я хотел бы вывести строку, следующую за найденной строкой.
Например, предположим, что искомый текст находится abc
в abc
строке 12. Я хотел бы вывести также и строку 13.
решение1
Если вы используете систему Linux, вы можете попробовать:
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.
Вы также можете использовать awk как:
awk '/C02/{print;getline;print}' ~/temp/log.txt