Perhaps because the OP mentions sed. That does not deserve a down vote in my opinion. That solution was even the best fitting my needs, then upvoted. I upvoted. Zombo Zombo 5, 5 5 gold badges 39 39 silver badges 58 58 bronze badges. GAD3R I get "awk: fatal: can't open source file 'inplace' for reading" GNU Awk 4.
Joseph R. The question is not about the -i switch. How do you figure that? It is an answer to the question. Yes, it's similar to the accepted answer, but not exactly the same. G-Man - one might argue the template used by cas is far from ideal but really, this is similar like in nearly identical As far as I am concerned they are technically identical as they are both gnu sed specific and it just happens that gnu sed will accept the i without a backslash.
The next line or multiple lines is literal text to be added. The x causes Ex to save the changes to the file and exit. Wildcard Wildcard If the file has no lines i.
ImHere ImHere What if there is no lines in the file? Justalearner The title of the question asks for: insert text before the first line of a file , so, a first line must already exist. That is also a problem for the selected answer. Amazing answer Isaac. This is certainly the only solution which works in the literal sense. I needed to add "schema": before the first line without any new line to be added. Awk Awk 11 4 4 bronze badges.
Splinter Splinter 19 1 1 bronze badge. Not sure what you're talking about There's only one sed answer that uses replacement but it doesn't substitute the existing line. Hao Liu Hao Liu 1 1 1 bronze badge. I vertically aligned the text outputs to make the code represent what is expected in the result. The result is converting command output to valid JSON. Bruno Bronosky Bruno Bronosky 3, 2 2 gold badges 24 24 silver badges 22 22 bronze badges.
While I haven't voted on your question, note that we already have several answers, including an accepted one, that demonstrate the desired result. JeffSchaller The Stack Exchange community is not a forum. Its purpose extends beyond helping the OP get unblocked. As you can see above, the new text was added on a new line at the beginning of the file.
To add the text at the beginning of the existing first line, use the -n argument of echo. We will be using particular features of sed to add text to the beginning of a file.
The -i argument tells sed to modify the file itself. Without it, sed automatically creates a copy of the file and performs all modifications on the copy. Again, this is adding the new content on a new line at the beginning. To add new text on the existing line, run:. The '1' at the beginning signifies that sed should consider the null string at beginning of line 1 and the 's' is a sed command, to replace a string.
We learned two simple yet catchy ways to add new text to the beginning of a file in Linux. Of course, the obvious way to do this, if it is to be done manually, is to modify the file with a file editor.
However, in a script, the methods discussed here can be used. I tested the echo and cat examples but could only get it to sort of work by using back-ticks instead of single quotes when assigning the x variable.
The resulting file had all of the new-line characters removed and displayed it on a single line. Sometimes safety is better than efficiency. If any errors occurred, with the examples you posted, you could lose the entire original file. You are right, there could be a limit on the size of variables. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 12 years, 8 months ago. Active 12 years, 8 months ago.
Viewed 3k times. Add a comment. Active Oldest Votes. OpenTextFile "test. WriteLine "Blaaa" f. WriteLine "Blaaaa some more Jose Basilio Jose Basilio
0コメント