
テキスト ファイルの内容を別のテキスト ファイルの先頭に追加するにはどうすればよいですか?
という名前のファイルが 3 つありますf1
f2
f3
。 3 つすべてにテキスト コンテンツが含まれています。
ファイルの内容をf1
ファイルの先頭に追加しf3
、ファイルの内容をf2
ファイルの末尾に追加するにはどうすればよいですかf3
?
答え1
コマンドを使用しますcat
。あなたの例を使用すると、cat f1 f3 f2
はファイルを連結して、f1、f3、f2 のように読み取ります。 に出力されます。stdout
したがって、f3 をそのように読み取りたい場合は、一時ファイルにリダイレクトしてから、そのファイルを f3 に移動します。cat f1 f3 f2 > tmp ; mv tmp f3
答え2
一時ディレクトリを作成...温度をタッチ
cat f1 >> temp .. temp now has the content of f1
cat f3 >> temp .. temp now has content of f1 and then f3
cat f2 >> temp .. temp now has the content in following order.. f1 f3 f2
mv temp f3 .. now file f3 contains f1 ..f3..f2