リストを検索する順序で別のファイルのリストから文字列を含むテキストファイルの行を抽出するにはどうすればよいですか?

リストを検索する順序で別のファイルのリストから文字列を含むテキストファイルの行を抽出するにはどうすればよいですか?

ファイル1:ソースfile.txt

Hello, It's the beginning of the sentence. 
it is the beginpoint of my career.
The end is always far.
We can start our beginpoint anytime we want.
The time we utilise to make our life good should be more.
This text doesn't mean anything.
I am writing this to include my three points:
beginpoint
time
end

ファイル2:string.txt

beginpoint
end
time

希望の出力:

it is the beginpoint of my career
We can start our beginpoint anytime we want.
beginpoint
The end is always far.
end
The time we utilise to make our life good should be more.
time

使った

grep -w -F -f  strings.txt sorcefile.txt > outputfile.txt

私は出力を取得します:

it is the beginpoint of my career.
The end is always far.
We can start our beginpoint anytime we want.
The time we utilise to make our life good should be more.
beginpoint
time
end

したがって、行は好きなだけですが、ソースファイルと同じ順序ではなく、クエリ順序でグループ化したいと思います。

ベストアンサー1

grep1つの方法は、1行に1回呼び出すことです。strings.txt

$ while IFS= read -r line; do grep -wF "$line" sourcefile.txt; done < strings.txt
it is the beginpoint of my career.
We can start our beginpoint anytime we want.
beginpoint
The end is always far.
end
The time we utilise to make our life good should be more.
time

strings.txtファイルが長すぎると速度が遅くなることがあります。 シェルループを使用してテキストを処理するのはなぜ悪い習慣と見なされますか?


sedフラグをサポートする場合e:

$ sed 's/.*/grep -wF '"'&'"' sourcefile.txt/e' strings.txt
it is the beginpoint of my career.
We can start our beginpoint anytime we want.
beginpoint
The end is always far.
end
The time we utilise to make our life good should be more.
time

おすすめ記事