Possible Duplicate:
Inserting text at the beginning of a file with sed via the terminal in Linux

I need to add a line to the beginning of a file. The line in question is

\def\submit{}

I've got a semi-working solution using sed. I don't know sed, but got it from somewhere on the net, but it doesn't work quite right, because it inserts an unwanted space at the beginning of the line. While this doesn't really matter, I figure I might as well do this right.

sed -i '1i\ \\\def\\\submit{}' 'dirname/filename'

It seems from my reading that all those extra backslashes are required to escape the shell. Other solutions are also welcome, but I'd like a comparably compact one liner if possible. Thanks.

This question, Inserting text at the beginning of a file with sed via the terminal in Linux is similar, but doesn't help me debug my expression.

EDIT: After this question was helpfully closed, I was able to get a working version of this by using an answer to "How can I prepend a tag to the beginning of several files?" I was not previously aware of this question. The solution is

sed -i '1s/^/\\\def\\\submit{}\\n/' 'dirname/filename'

I still don't know why the original version didn't work. I guess I should learn the sed syntax, but really I just want something that works.

I'm posting here, since I cannot post this as an answer to this question. BTW, I think How can I prepend a tag to the beginning of several files? would be a better question to reference than the Inserting text at the beginning of a file with sed via the terminal in Linux which this question was linked to. It has a lot more answers.

link|improve this question

feedback

closed as exact duplicate by Gilles, Michael Mrozek Sep 22 '11 at 3:08

This question covers exactly the same ground as earlier questions on this topic; its answers may be merged with another identical question. See the FAQ for guidance on how to improve it.

2 Answers

sed is for editing streams -- a file is not a stream. Use a program that is meant for this purpose, likeed or ex. The -i option to sed is not only not portable, it will also break any symlinks to your file, since it essentially deletes it and recreates it, which is pointless.

ed -s [file] << EOF
0a
\\def\\submit{}
.
w
EOF
link|improve this answer
jw013 suggests that you mean hard links, not symlinks – Michael Mrozek Sep 21 '11 at 20:40
1  
@Michael -- I don't. Try creating a file, then creating a symlink to that file, then passing the symlink as the file for sed -i to operate on. – Chris Down Sep 21 '11 at 20:49
@ChrisDown Just saw this again. You are indeed correct about -i removing symlinks, although GNU sed provides the --follow-symlinks option for that case. Of course all of this is rendered moot by the more important point that sed -i is non-portable and should be avoided anyways. – jw013 Mar 22 at 16:03
feedback

I don't have access to sed right now, so I'm not entirely sure this is correct, but I think you just need to remove the first backslash and the space following it.

sed -i '1i\\\def\\\submit{}' 'dirname/filename'

Try it and see if that works.

link|improve this answer
feedback

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