echoコマンドを使用して最後の行の前に複数の行を追加する方法

echoコマンドを使用して最後の行の前に複数の行を追加する方法
{  
  This is test1  
  this is test2  
  this is test3  
}

ここで、echoコマンドを使用して最後の行の前に複数の行を追加したいと思います。 ! ! !

私の出力は次のとおりです

{  
  This is test1  
  this is test2  
  this is test3  
  this is test4  
  this is test5  
}  

使用エコsed以外のコマンドまたはawkコマンド

ベストアンサー1

sed何らかの理由で使用したくない場合でも、これを行う正しい方法ですsed

sedスクリプトは次のとおりです。

$i\
  this is test4\
  this is test5

sed -f script.sed fileこのiコマンドは、アドレス指定された行の前に行を挿入し、$ファイルの最後の行を指定します。

GNUを使用する「1つのライナー」でsed

$ sed -e '$i\' -e '  this is test4\' -e '  this is test5' file
{
  This is test1
  this is test2
  this is test3
  this is test4
  this is test5
}

ファイルが実際にJSONファイルか他の構造化テキスト形式であるかに応じて、ファイルjq操作に適した同様のツールがあるかもしれません。


要求どおりに使用しますecho(オプションは通常負数を使用しないため、headGNU coreutilsを使用すると仮定します)。-n

{   head -n -1 file
    echo '  this is test4'
    echo '  this is test5'
    tail -n 1 file; } >newfile

おすすめ記事