The command inserts a tag to the beginning of a file:

sed -i "1s/^/<?php /" file

How can I insert something to the end of each file with SED?

migrated from superuser.com Sep 12 '11 at 22:28

This question came from our site for computer enthusiasts and power users.

  • 3
    { cat file; echo 'END OF FILE'; } > newFile , this works also thanks to sh.*. (not sed but works and simple) – user2362 Dec 12 '11 at 14:20
  • 4
    a simple echo "Cool Text" >> file should also do the trick. – Danpe Jun 22 '15 at 15:10

sed -e "\$aTEXTTOEND" is the simplest way.

How it works

$ matches the last line (it's a normal sed address; 4aTEXTTOEND would insert after the fourth line), a is the append command, and TEXTTOEND is what to append.

  • Escape $ as $$ in a Makefile. – stevesliva Dec 3 '14 at 1:22
  • For OS X sed -e '$a\NEWLINE\\'. Replace NEWLINE with an actual new line. – Jorge Bucaran Oct 18 '15 at 15:47
  • 13
    sed -i -e "\$aTEXTTOEND" <filename> would be the full command. – Lasse Meyer Jul 29 '16 at 19:52
  • 2
    not working on macos. Not even Jorge Bucaran comment: sed: 1: "$a\NEWLINE\\ ": extra characters after \ at the end of a command – Emanuele Paolini Aug 15 '18 at 16:02
  • 1
    In debian 9 it adds a new line instead of adding at the end of the last line. – Luciano Andress Martini Sep 18 '18 at 13:29

No need to use sed in that case. How about

echo "<?php" >> file
  • 2
    True enough, but you can guess that the OP's use case is bracketing the file with "<php " and ">", and quoting selected contents all in one go, so he probably really does want a sed fragment. – dmckee Sep 6 '09 at 15:12
  • 2
    Also if you'll do this and don't want a newline, use echo -n – Vinko Vrsalovic Sep 6 '09 at 15:15
  • 2
    I don't like to guess. Each time I try, I'm wrong. – innaM Sep 6 '09 at 15:38
  • 4
    Another reason why the accepted answer is more useful is in the case of prepending this with sudo, which causes this variation to fail since echo is a built-in shell command rather than an executable file found in $PATH – Tony Cesaro Oct 28 '14 at 15:33
  • @TonyCesaro: echo is a built-in but is also a executable file found in $PATH – Zan Lynx Dec 28 '16 at 12:51

Assuming you want to put the ending php tag to the files, then

sed -i '$s/$/\n?>/' file

should do the trick

  • 1
    This works nicely in cases where arbitrary manipulation of the last line of input is desired. Thanks! – Jay Taylor Jul 1 '15 at 23:17
  • A decomposed explanation of the command would be greatly appreciated – GGhe Aug 1 at 21:57

See your original post for the all in one sed command.

find . -type f -exec sed -i -e "1s/^/<?php /" -e "\$s/\$/ ?>/" {} \;

Use sed 'append (a)' command. For example, to append two lines at the end of file:

sed -i -e '$a\
foo1 bar1\
foo2 bar2' file

In a simple way to write and make the changes in the contents at a specific line in a file . If you want to insert anything at a specific line number then below command can be used:

sed -i '2i i have started' sheikh.log

where 2i - line number 2 and i stands for inserting . If you have to insert at the last line then use $ in place of 2 . i have started - text to be inserted and sheikh.log is the filename .

We can also make the changes in the line by using the below command

sed -i '2c we have started' sheikh.log

i is changed to we .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy