選択した情報をあるTXTファイルから別のTXTファイルに移動する

選択した情報をあるTXTファイルから別のTXTファイルに移動する

config.txt と template.txt という 2 つの txt ファイルがあります。

template.txtはconfig.txtから関連情報を取得する必要があります。

config.txtに次のものが含まれているとします。

colour1: red
colour2: blue

template.txt には以下が含まれます。

colour1:
colour2:

これら2つのファイルを「リンク」して、template.txtにconfig.txtの対応するヘッダーから情報を取得させることは可能ですか?

テンプレート.txt

colour1: red
colour2: blue

ベストアンサー1

grep -fユースケースに応じてファイルからconfig.txt行を検索するtemplate.txtと、一致する行を取得できます。

つまり:

$ cat config.txt
color1: red
color2: blue
color3: green
color4: purple
color5: orange
foo: bar
$ cat template.txt
color1:
color2:
color5:
$ grep -f template.txt config.txt
color1: red
color2: blue
color5: orange

参考にしてくださいできないgrep -f template.txt config.txt > template.txt実行する前にシェルが消去されるので、これを実行してくださいtemplate.txtgrepこの問題を解決するには、一時ファイルを使用する必要があります。

おすすめ記事