![ファイルの最初の行に新しいテキスト行を追加するには? [コピー]](https://linux33.com/image/87801/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%AE%E6%9C%80%E5%88%9D%E3%81%AE%E8%A1%8C%E3%81%AB%E6%96%B0%E3%81%97%E3%81%84%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88%E8%A1%8C%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%99%E3%82%8B%E3%81%AB%E3%81%AF%EF%BC%9F%20%5B%E3%82%B3%E3%83%94%E3%83%BC%5D.png)
文書:
TABLE1
1234
9555
87676
2344
予想出力:
Description of the following table:
TABLE1
1234
9555
87676
2344
答え1
実際に必要なものを実行するのに十分ですecho
。cat
echo "Description of the following table:" | cat - file
このパラメータはから読み込むように-
指示します。cat
stdin
答え2
そしてsed
:
$ sed -e '1i\
Description of the following table:
' <file
Description of the following table:
TABLE1
1234
9555
87676
2344
答え3
printf "%s\n" 1 i "Description of the following table:" . w | ed filename
コマンド(1行に1つずつ)を出力しますprintf
。 ed
ed filename
ed
指示に従ってファイルを編集します。
1 # go to line 1
i # enter insert mode
Description of the following table: # text to insert
. # end insert mode
w # write file to disk
ちなみに、他のほとんどのテキスト編集ツールと同様に、一時ファイルに書き込んで移動するのではなく、真のed
内部編集を実行します。sed
編集されたファイルはファイルシステムで同じinodeを保持します。
答え4
awk オプションは次のとおりです。
gawk '
BEGIN{print "Description of the following table:"}
{print $0}' file > temp && mv temp file
sedにはファイルに直接書き込むことができる内部編集オプション-iがあるので、これはsedよりも少し作業があります。