文字列の組み合わせの長いリストからさまざまな文字列を含むすべてのファイルを見つけるにはどうすればよいですか?

ViolaW

私はまだコマンドラインツール(Mac OSXターミナルを使用)に非常に慣れていないので、どこかで答えを見逃していないことを願っていますが、何時間も検索しました。

3つの文字列の200の組み合わせを含むテキストファイル(strings.txtと呼びましょう)があります。[編集2017/01/30 ]最初の5行は次のようになります。

"surveillance data" "surveillance technology" "cctv camera"
"social media" "surveillance techniques" "enforcement agencies"
"social control" "surveillance camera" "social security"
"surveillance data" "security guards" "social networking"
"surveillance mechanisms" "cctv surveillance" "contemporary surveillance"

1行目の監視データのようなバイグラム/ 2ワードのフレーズが一緒になっている限り、strings.txtを他の形式に変更できることに注意してください。(つまり、以下の@MichaelVehrsによる回答については、必要に応じて引用符を削除できます)。

ここで、800を超えるファイルのディレクトリで、(ファイル内の任意の場所に)少なくとも1つの文字列の組み合わせを含むファイルを検索したいと思います。私の最初のアイデアは、次のようなパターンファイルでegrepを使用することでした。

egrep -i -l -r -f strings.txt file_directory

ただし、これを機能させるには、1行に1つの文字列がある場合のみです。特定のパターンの3つの文字列すべてを含む識別されたファイルが必要なため、これは望ましくありません。grepパターンファイルに何らかのAND演算子を追加する方法はありますか?または、別の関数/ツールを使用して目的を達成する別の方法はありますか?どうもありがとう!

2017/01/30を編集

以下の@MichaelVehrsによる回答は非常に役に立ちました。私はそれを次のように編集しました:

while read one two three four five six
do grep -ilFr "$one $two" *files* | xargs grep -ilFr "$three $four" |  xargs grep -ilFr "$five $six"
done < *patternfile* | sort -u

この回答は、パターンファイルに引用符のない文字列が含まれている場合に機能します。残念ながら、パターンファイルの最初の行のパターンとのみ一致しているようです。誰かが理由を知っていますか?

2017/01/29を編集

複数の値のgrepについて同様の質問が以前行われていANDますが、他のファイルのパターンファイルstrings.txtの3つの文字列の組み合わせの1つと一致させるためロジックが必要です。マッチングが機能するためにはstrings.txtの形式を変更する必要があるかもしれないことを理解しており、提案をいただければ幸いです。

ジョージヴァシリオウ

agrepシステムに存在しないように思われるため、sedとawkに基づいたこの代替手段を調べて、ローカルファイルによって読み取られたパターンからgrepと操作を適用してください。

PS:osxを使用しているので、お持ちのawkバージョンが以下の使用法をサポートするかどうかはわかりません。

awk この使用法では、複数のパターンのAND演算を使用してgrepをシミュレートできます。
awk '/pattern1/ && /pattern2/ && /pattern3/'

したがって、これからパターンファイルを変換できます。

$ cat ./tmp/d1.txt
"surveillance data" "surveillance technology" "cctv camera"
"social media" "surveillance techniques" "enforcement agencies"
"social control" "surveillance camera" "social security"
"surveillance data" "security guards" "social networking"
"surveillance mechanisms" "cctv surveillance" "contemporary surveillance"

これに:

$ sed 's/" "/\/ \&\& \//g; s/^"/\//g; s/"$/\//g' ./tmp/d1.txt
/surveillance data/ && /surveillance technology/ && /cctv camera/
/social media/ && /surveillance techniques/ && /enforcement agencies/
/social control/ && /surveillance camera/ && /social security/
/surveillance data/ && /security guards/ && /social networking/
/surveillance mechanisms/ && /cctv surveillance/ && /contemporary surveillance/

PS:>anotherfile最後にを使用て出力を別のファイルにリダイレクトするか、sed -iオプションを使用して同じ検索語パターンファイルにインプレース変更を加えることができます。

次に、このパターンファイルからawk形式のパターンをawkにフィードする必要があります。

$ while IFS= read -r line;do awk "$line" *.txt;done<./tmp/d1.txt #d1.txt = my test pattern file

次のように、この元のパターンファイルの各行にsedを適用して、元のパターンファイルのパターンを変換することもできませんでした。

while IFS= read -r line;do 
  line=$(sed 's/" "/\/ \&\& \//g; s/^"/\//g; s/"$/\//g' <<<"$line")
  awk "$line" *.txt
done <./tmp/d1.txt

またはワンライナーとして:

$ while IFS= read -r line;do line=$(sed 's/" "/\/ \&\& \//g; s/^"/\//g; s/"$/\//g' <<<"$line"); awk "$line" *.txt;done <./tmp/d1.txt

上記のコマンドは、次のような正しいAND結果をテストファイルに返します。

$ cat d2.txt
This guys over there have the required surveillance technology to do the job.
The other guys not only have efficient surveillance technology, but they also gather surveillance data by one cctv camera.

$ cat d3.txt
All surveillance data are locked.
All surveillance data are locked and guarded by security guards.
There are several surveillance mechanisms (i.e cctv surveillance, contemporary surveillance, etv)

結果:

$ while IFS= read -r line;do awk "$line" *.txt;done<./tmp/d1.txt
#or while IFS= read -r line;do line=$(sed 's/" "/\/ \&\& \//g; s/^"/\//g; s/"$/\//g' <<<"$line"); awk "$line" *.txt;done <./tmp/d1.txt
The other guys not only have efficient surveillance technology, but they also gather surveillance data by one cctv camera.
There are several surveillance mechanisms (i.e cctv surveillance, contemporary surveillance, etv)

更新:
上記のawkソリューションは、一致するtxtファイルの内容を出力します。
内容の代わりにファイル名を表示する場合は、必要に応じて次のawkを使用します。

awk "$line""{print FILENAME}" *.txt

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ