如何將一個文字檔案的內容新增到另一個文字檔案的開頭?
有 3 個名為f1
f2
f3
.這三個都有一些文字內容。
如何將文件內容新增f1
至文件開頭f3
並將文件內容新增f2
至文件末端f3
?
答案1
使用cat
命令。使用您的範例,cat f1 f3 f2
將 con*cat*enate 這些檔案放在一起,以便它讀起來像 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