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?

link|improve this question

63% accept rate
{ cat file; echo 'END OF FILE'; } > newFile , this works also thanks to sh.*. (not sed but works and simple) – hhh Dec 12 '11 at 14:20
feedback

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

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

4 Answers

up vote 6 down vote accepted

-e "\$aTEXTTOEND" is the simplest way

link|improve this answer
Wow! You rock, this is a hit to the back. Why and how does it actually work? Some zero-width thing? – hhh Dec 12 '11 at 14:00
1  
@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
feedback

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

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

should do the trick

link|improve this answer
feedback

No need to use sed in that case. How about

echo "foo bar" >> file

?

link|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
feedback

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/\$/ ?>/" {} \;
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.