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.
>outputfileafter the command, e.g.sed '....' mytextfile >outputfile. If you want to update the existing file use-ie.g.sed -i '...' mytextfileorsed -i.bak ... mytextfileif you want to make a backup copy of your original file.sed '1s/^/\/\/\'$'\n/' mytextfile(escaping means adding a backslash before the character has to be escaped). For the\'$'\npart, see this.