LoginSignup
1
0

More than 1 year has passed since last update.

Shell | 複数のキーワードが含まれるファイルを grep で一斉検索する ( find + xargs + grep )

Last updated at Posted at 2018-07-25

課題

first_textsecond_text の両方が含まれるファイルを一斉検索したい。

検証

両方のキーワードが含まれるものを用意する

example1.txt
first_text
second_text

片方のキーワードしか含まれないものを用意する

example2.txt
first_text
example3.txt
second_text

コマンド

$ find ./ -name "*.txt" | xargs -I %FILE% sh -c 'grep first_text >/dev/null "%FILE%" && grep second_text >/dev/null "%FILE%" && echo "%FILE%"'

  • find で見つかったファイル名を xargssh に渡している
  • grep の標準出力は捨てて、 true / false の判定だけをしている

結果

両方のキーワードが含まれる example1.txt だけが HIT した。

./example1.txt

環境

  • Ubuntu 16.04.2 LTS (Xenial Xerus)
  • Docker version 18.03.1-ce, build 9ee9f40

Gist

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

1
0
1

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
1
0