Adding text to beginning of text file

sedtext formatting

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.

Best Answer

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   
Related Question