LoginSignup
12
14

More than 5 years have passed since last update.

grepコマンドで複数の条件を指定して文字列を検索するには

Last updated at Posted at 2016-02-01

実現したいこと

  • grepコマンドで複数の条件を指定して文字列を検索する

  • body.txt

123 ba1f2511fc30423bdbb183fe33f3dd0f
456 d2d362cdc6579390f1c0617d74a7913d
789 aa3f5bb8c988fa9b75a1cdb1dc4d93fc
000 23ebe88c0224f4f4fc0b608216e98735
111 1181c1834012245d785120e3505ed169

具体的には、上記のようなテキストファイルをgrepする際に、 123 と 456 を含む行のみを検索し検索結果を表示させたい。

実現方法および解決方法

grepコマンドラインオプションの -e を利用すると複数条件で検索できる。

実行コマンド

grep -e 検索文字列 -e 検索文字列
  • -e オプションで次々に検索文字列を付け足していきます。
$ grep -e 123 -e 456 body.txt
123 ba1f2511fc30423bdbb183fe33f3dd0f
456 d2d362cdc6579390f1c0617d74a7913d
$

情報源

man grep

補足

12
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
14