2
{  
  This is test1  
  this is test2  
  this is test3  
}

Now i want append multiple lines before the last line using echo command Anyone please help me!!!!

My output is look like below

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

Using Echo Commands not sed or awk

Arushix
  • 1,290

2 Answers2

3

sed is the correct way of doing this, even if you said that you for some reason don't want to use sed.

The sed script would look like

$i\
  this is test4\
  this is test5

and you would run that as sed -f script.sed file. The i command inserts lines before the addressed line, and the $ addresses the last line of the file.

As a "one-liner" using GNU 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
}

Depending on whether the file is actually a JSON file or in some other structured text format, there may be tools like jq that are better suited for manipulating it.


To use echo as you requested (this also assumes that you are using head from GNU coreutils as the -n option does not usually take a negative number):

{   head -n -1 file
    echo '  this is test4'
    echo '  this is test5'
    tail -n 1 file; } >newfile
Kusalananda
  • 333,661
0

You can use head with echo to achieve this

  cat <outputfilename> | head -n -1  && echo -e "  this is test4\n  this is test5\n  this is test6\n}"

If you want to append the output in a file , just use ">" output redirection

  (cat <outputfilename> | head -n -1  && echo -e "  this is test4\n  this is test5\n  this is test6\n}") > <RESULTFILENAME>
Arushix
  • 1,290