Tabish Rizvi
Tabish Rizvi

Reputation: 367

How to add multiple newlines at end of a file in ubuntu using sed

Input File

ABC
DEF

Output File

ABC
DEF


GHI

I am using

sed -i -e '$a\\GHI ' input.txt

but it adds GHI but not 2 preceding new lines

ABC
DEF
GHI

Upvotes: 2

Views: 1080

Answers (3)

potong
potong

Reputation: 58410

This might work for you (GNU sed):

sed '$!b;G;G;aGHI' file

Append a couple of newlines to the pattern space, then append GHI to the output stream.

N.B. The pattern space is printed before the output stream.

or explicitly:

sed '$G;$G;$aGHI' file

Upvotes: 0

peter__barnes
peter__barnes

Reputation: 351

use \n to change line for example,a file 1.txt

11111
222222

sed -i '$s/$/\n\n\n333333\n4444444444/' 1.txt

explain:first '$' means last line of file,second '$' means end of line.

result:

11111
222222


333333
4444444444

Upvotes: 0

Sundeep
Sundeep

Reputation: 23667

Since you've tagged linux, am assuming you have GNU sed and that \n is recognized

$ cat ip.txt 
ABC
DEF
$ sed '$a\\n\nGHI' ip.txt
ABC
DEF


GHI

Add -i option once it is working


GNU sed doesn't require \ to be used after a command but has its own use cases. Hence the \\n at start in previous example. See GNU sed manual for details

$ echo 'foo' | sed '$a123'
foo
123
$ echo 'foo' | sed '$a 123'
foo
123
$ echo 'foo' | sed '$a\ 123'
foo
 123


Also, this can be done easily without needing sed

$ printf '\n\nGHI\n' >> ip.txt 
$ cat ip.txt 
ABC
DEF


GHI

Upvotes: 3

Related Questions