0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

sedで改行を追加できなかったのでcatとechoで対応した話

Posted at

概要

sedコマンドで先頭行に文字列と改行を追加しようとしたところ、改行がどうしても追加できなかったのでcatとechoコマンドで対応した時の方法です。

sedコマンドで実行しようとしたこと

sed -i '' -e '1s/^/追加文字列\n/' (ファイルパス)

上記では文字列は追加できるが、改行だけ改行として認識されず、nが出力されてしまう。

catとechoで対応した方法

# catコマンドで変数にファイルの内容を入れる  
$C=cat (ファイルパス)  

# echoコマンドで挿入したい文字列と$Cを連結させてファイルに入れる
echo "追加テキスト\n${C}" >> (ファイルパス)

まとめ

sedコマンドで改行を挿入する方法として、gnu-sedを入れるなどの方法があったが、うまく使いこなせなかったのでもう少し調査をしようと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?