Thursday, November 3, 2011

sed add line at end of file

If you want to add a a line of text to the end of a file, and want to do the edit in place, use this syntax:

sed -i '$ a\This is some sample text.'  /foo/bar

2 comments:

E Tar said...

how do i copy first three lines of a file and append it to end of the file
eg i have a file eg.txt
line1
line2
line3
line4

i want the output to be
line1
line2
line3
line4
line1
line2
line3

John said...

$ echo -e "line1\nline2\nline3\nline4" > eg.txt

$ cat eg.txt
line1
line2
line3
line4

$ echo -e "line1\nline2\nline3" >> eg.txt

$ cat eg.txt
line1
line2
line3
line4
line1
line2
line3