Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

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

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

    
{ 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
add comment

5 Answers 5

up vote 11 down vote accepted

-e "\$aTEXTTOEND" is the simplest way

share|improve this answer
    
Wow! You rock, this is a hit to the back. Why and how does it actually work? Some zero-width thing? –  user2362 Dec 12 '11 at 14:00
2  
@hhh $ 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 –  Michael Mrozek Dec 12 '11 at 14:26
add comment

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

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

should do the trick

share|improve this answer
add comment

No need to use sed in that case. How about

echo "foo bar" >> file

?

share|improve this answer
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
    
I don't like to guess. Each time I try, I'm wrong. –  innaM Sep 6 '09 at 15:38
add comment

See your original post http://superuser.com/questions/36575/unix-add-a-tag-to-the-beginning-of-each-file for the all in one sed command.

find . -type f -exec sed -i -e "1s/^/<?php /" -e "\$s/\$/ ?>/" {} \;
share|improve this answer
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.