1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

while read を使用して、1行ずつ読み込み、一致する場合のみ出力させる

Posted at

背景

既存のあるシェルスクリプトのSIテストをしていたところ、引数設定に漏れがあることを見つけた。そのため、その他のスクリプトにも同様の事象がないか確認する作業が必要となった。
数が多く検索をかけるのは時間がかかるため、シェルスクリプトを作成して作業時間の節約を行うことにした。

準備

検索対象の文字列が既にまとめられていたため、その対象文字列をサクラエディタに入力した。また、使用するtoolはlinuxであったため、改行コードはLFに変更し、保存した。

書式

while read line
 do 
     grep -r "$line" パス名
 done < ファイル名

処理の流れ

①"done < ファイル名"の部分で、ファイル名に記載されている文字列を読み込み、変数lineに格納する。
②grepにより、変数lineへ格納した文字列と指定したパス配下に存在するファイルの中に一致する文字列がある場合、出力される。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?