【個人的なメモ】 grep で指定した文字列の前後の行を表示する

Linux システムでは、grep を使用して指定されたコンテンツを表示できます。
例: grep "123" test.log //test.log に 123 文字を含むログを表示します。

指定した内容の上位行と下位行を表示したい場合は、次の使用方法を参照してください。

$grep -10 '123' test.log//一致する行の前後 10 行を出力する
または
$grep -C 10 '123' test.log//一致する行の前後 10 行を出力する
または
$ grep - A 10 -B 10 ' 123' test.log //一致する行の前後 10 行を出力します

$grep -A 10 '123' test.log //一致する行の最後の 10 行を出力します

$grep -B 10 '123' test.log//一致する行の最初の 10 行を出力します

その他の例:
//「123」と「456」の両方に一致する行を表示
grep '123' test.log | grep '456'

//test.logで123を満たす内容の行番号を検索
grep -n '123' test.log

//test.log の指定された行番号以降の内容を表示します (例: 50 行
tail -n +50 test.log)

//test.log の 50 行目から 100 行目を表示
sed -n '50,100p' test.log#文字 p を覚えておいてください

詳しい使い方はhttp://blog.csdn.net/lychbeyond/article/details/41042483で確認できます。

おすすめ

転載: blog.csdn.net/m0_49303490/article/details/128052914