117

This sed 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?

4
  • 3
    { cat file; echo 'END OF FILE'; } > newFile , this works also thanks to sh.*. (not sed but works and simple)
    – user2362
    Commented Dec 12, 2011 at 14:20
  • 9
    a simple echo "Cool Text" >> file should also do the trick.
    – Danpe
    Commented Jun 22, 2015 at 15:10
  • stackoverflow.com/a/30219386/385273
    – Ben
    Commented Jan 3, 2020 at 15:39
  • 1
    Seems the implementation of sed differs on macOS. @Danpe echo alternative works nicely. Commented Sep 14, 2023 at 14:26

8 Answers 8

164

The simplest way with GNU sed:

sed -i -e '$aTEXTTOEND' filename

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, filename is the file where the TEXTTOEND will be inserted

sed on macOS

This is a GNU extension and does not work in the same way in the macOS version of sed. You can install gsed with Homebrew.

8
  • 1
    Escape $ as $$ in a Makefile.
    – stevesliva
    Commented Dec 3, 2014 at 1:22
  • 3
    this command is not support a new file. ( zero byte)
    – zw963
    Commented Nov 11, 2016 at 15:07
  • 3
    not working on macos. Not even Jorge Bucaran comment: sed: 1: "$a\NEWLINE\\ ": extra characters after \ at the end of a command Commented Aug 15, 2018 at 16:02
  • 2
    In debian 9 it adds a new line instead of adding at the end of the last line. Commented Sep 18, 2018 at 13:29
  • 4
    For MacOS sed: -e $'$a\\\n\\'
    – Rob
    Commented Aug 11, 2022 at 20:50
41

No need to use sed in that case. How about

echo "<?php" >> file
4
  • 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. Commented Sep 6, 2009 at 15:12
  • 2
    Also if you'll do this and don't want a newline, use echo -n Commented Sep 6, 2009 at 15:15
  • 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 Commented Oct 28, 2014 at 15:33
  • @TonyCesaro: echo is a built-in but is also a executable file found in $PATH
    – Zan Lynx
    Commented Dec 28, 2016 at 12:51
31

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

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

should do the trick

3
  • 1
    This works nicely in cases where arbitrary manipulation of the last line of input is desired. Thanks!
    – Jay Taylor
    Commented Jul 1, 2015 at 23:17
  • 1
    A decomposed explanation of the command would be greatly appreciated
    – GGhe
    Commented Aug 1, 2019 at 21:57
  • 3 years later. sed -i (edit file in place) '$ (when at the end of the file) s (sed command for search and replace, searches with regexp like so s/findme/replacewth/) so s/$ ($ regex end of line) /\n?>/ the replacewith is \n?> so replace the end of the line with (newline ?> which is a closing tag for php
    – Joshua
    Commented Jun 2, 2023 at 16:07
6

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
5

The equivalent of your sed -i '1s/^/<start</' file would be:

sed -i '$s/$/>end>/' file

That will add the text >end> to the last line (without newlines) of the file. Of course, in this case (as in your example also) it needs that the file contains at least one line (not empty).

Using double quotes (as in the selected answer) is not a good idea as then the shell will try to process each $ in the string. The command will convert to:

sed -i "\$s/\$/>end>/" file

Using a (append, end) or i (insert, start) will also insert a newline, demand different calls in BSD and GNU and still need a non-empty file.

For GNU:

$ sed '1i<start<' file
$ sed '$a>end>' file

For BSD:

$ sed '1i\
<start<' file
$ sed '$a\
>end>' file

Or, in shells that allow it (C-string):

$ sed $'1i\\\n<start<' file
$ sed $'$a\\\n>end>' file

Of course, nothing is simpler than:

echo '>end>' >> file

which works with empty files as well.

5

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

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

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 .

0

First and Last

I would assume that anyone who searched for how to insert/append text to the beginning/end of a file probably also needs to know how to do the other also.

cal |                            \
  gsed -E                        \
       -e     '1i\{'             \
       -e     '1i\  "lines": ['  \
       -e 's/(.*)/    "\1",/'    \
       -e '$s/,$//'              \
       -e     '$a\  ]'           \
       -e     '$a\}'

Explanation

This is cal output piped to GNU sed (called gsed on macOS installed via brew.sh, and simply sed on GNU installations) with extended RegEx (-E) and 6 "scripts" applied (-e) and line breaks escaped with \ for readability.

  • Scripts 1 & 2 use 1i\ to "at line 1, insert".
  • Scripts 5 & 6 use $a\ to "at line <last>, append". I vertically aligned the text outputs to make the code represent what is expected in the result.
  • Scripts 3 & 4 do substitutions (the latter applying only to "line <last>").

The result is converting command output to valid JSON.

Output

{
  "lines": [
    "    October 2019      ",
    "Su Mo Tu We Th Fr Sa  ",
    "       1  2  3  4  5  ",
    " 6  7  8  9 10 11 12  ",
    "13 14 15 16 17 18 19  ",
    "20 21 22 23 24 25 26  ",
    "27 28 29 30 31        ",
    "                      "
  ]
}
2
  • While I haven't voted on your question, note that we already have several answers, including an accepted one, that demonstrate the desired result.
    – Jeff Schaller
    Commented Oct 22, 2019 at 12:12
  • @JeffSchaller The Stack Exchange community is not a forum. Its purpose extends beyond helping the OP get unblocked. The goal is to seed the global knowledge base with many options that seekers can stumble upon when searching for answers. The reason I titled my answer is to help users recognize the utility of this answer before they even click the Google result. Commented Oct 22, 2019 at 16:09

You must log in to answer this question.