SED: insert text after the last line?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The simplest way is:

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

Method 2

No need to use sed in that case. How about

echo "<?php" >> file

Method 3

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

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

should do the trick

Method 4

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

Method 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.

Method 6

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

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

Method 7

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 .

Method 8

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        ",
    "                      "
  ]
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x