3

I am trying to add a line of text to the beginning of a text file (Actually not text, but just two backslashes). I am trying the following, which I found in this answer: Add lines to the beginning and end of the huge file

$ sed -i '1i\'"$\\" $Simdata.txt

However, I cannot seem to get it to function. When I use the above, I get the error:

sed: 1: ".txt": invalid command code .

I have tried without the txt ending, but then I get this error:

sed: -i may not be used with stdin

I also tried this line of code, which I found somewhere else:

$ sed -i '1i \\' Simdata.txt
sed: 1: "Simdata.txt": invalid command code S

UPDATE:

@don_crissti, your first solution is what I want to do. However, it does not seem to function as intended.

$ cat Simdata.txt
abcdefghijkabcdefghijk
//
abcdefghijkabcdefghijk
$ sed '1i\
> \\\\' Simdata.txt
\\abcdefghijkabcdefghijk
//
abcdefghijkabcdefghijk
$ cat Simdata.txt
abcdefghijkabcdefghijk
//
abcdefghijkabcdefghijk

So the file is not updated. I would prefer not to print the file, just update the file or print to a new one. Also, I made a silly typo: I would actually like to add two forward slashes. What I am ultimately aiming at doing is the following.

I have a file looking like this:

abcdefghijkabcdefghijk
//
abcdefghijkabcdefghijk
//
abcdefghijkabcdefghijk

I want to first add // to the beginning of the file:

//
abcdefghijkabcdefghijk
//
abcdefghijkabcdefghijk
//
abcdefghijkabcdefghijk

And then add a unique name after each //:

// text 1
abcdefghijkabcdefghijk
// text 2
abcdefghijkabcdefghijk
// text 3
abcdefghijkabcdefghijk

and save this to a new file, without changing the original file. So the above question was aiming at figuring out the first step.

  • 1
    If you want to print to another file just add >outputfile after the command, e.g. sed '....' mytextfile >outputfile. If you want to update the existing file use -i e.g. sed -i '...' mytextfile or sed -i.bak ... mytextfile if you want to make a backup copy of your original file. – don_crissti Jan 28 '15 at 9:50
  • Perfect! That seems to work! Thank you. However, it does not insert a new line, but just adds \\ to the beginning of the original first line. Do I need to add \n? – Hjalte Jan 28 '15 at 9:56
  • That doesn't work either. The files are mac-made. – Hjalte Jan 28 '15 at 10:07
  • Yes! That works! Can you explain the logic to me? If I want to insert // and not \\, which backslashes do I change to forward slashes in the code? – Hjalte Jan 28 '15 at 10:25
  • 1
    You just escape those as well, e.g. sed '1s/^/\/\/\'$'\n/' mytextfile (escaping means adding a backslash before the character has to be escaped). For the \'$'\n part, see this. – don_crissti Jan 28 '15 at 10:33
3

You have to escape the backslashes (so four backslashes will insert two literal backslashes):

sed '1i\
\\\\' my_text_file

or

sed '1s/^/\\\\\n/' my_text_file

The first one will insert a new line containing two backslashes at the beginning of your file, the second one will substitute the beginning of the first line (^) with two backslashes followed by a \newline (same result).
With osx sed:

sed '1s/^/\\\\\'$'\n/' mytextfile   
  • don_crissti, your first solution is what I am trying to do. I have updated my above question with the attempt to use your solution. – Hjalte Jan 28 '15 at 9:31
1

If you just want to add two \\ to the beginning of a file then this should work:

echo '\\' | cat - todo.txt > temp && mv temp todo.txt
1
echo '\\\\' > newfile.txt; cat oldfile.txt >> newfile.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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