Bash: inserting a line into a file
Johny jon
-
Lemez -
Lemez -
Hello everyone,
here I would like to insert a line at the very beginning of a file. I tried:
echo hello >> myfile.txt
but of course, it adds hello at the end of the file, while I want to add it right at the beginning of the file.
If you could provide me with some information, I would be grateful :)
Thanks in advance
Johny Jon
here I would like to insert a line at the very beginning of a file. I tried:
echo hello >> myfile.txt
but of course, it adds hello at the end of the file, while I want to add it right at the beginning of the file.
If you could provide me with some information, I would be grateful :)
Thanks in advance
Johny Jon
11 answers
-
Hello
old topic but among the top responses on the subject
sed inserting a line at the beginning of a file
since the response didn't seem clear to me
I will thus allow myself a few explanations
for any useful purpose
explanation using the magical
SED command
sed -i "1i\Adding this line at the beginning of the file\n" file.txt
sed = Stream EDitor
-i = apply directly to the file without a temporary file
Note: -i.BAK = creating a backup file file.txt.BAK
" = allows the use of escape characters
and interpretation of variables in the string.
Note: using ' (single quote) disables these functions.
1=line number before which the text will be inserted
\ = escape character that protects the text to be inserted
preventing the first character of the text from being interpreted
as a command/parameter -
Hello,
You can do this with the "sed" filter, it's very simple and all that c*n :[jp@Mandrake tmpfs]$ cat essai.txt Hello everyone, here I would like to insert a line right at the beginning of a file. I tried: echo hello >> myfile.txt but of course, it adds hello at the end of the file, while I would like to add it right at the beginning of the file. So if you could help me, I would be grateful :) Thanks in advance Johny Jon [jp@Mandrake tmpfs]$ sed -i '1iHello' essai.txt [jp@Mandrake tmpfs]$ cat essai.txt Hello Hello everyone, here I would like to insert a line right at the beginning of a file. I tried: echo hello >> myfile.txt but of course, it adds hello at the end of the file, while I would like to add it right at the beginning of the file. So if you could help me, I would be grateful :) Thanks in advance Johny Jon [jp@Mandrake tmpfs]$
Basically, to insert text before a line, you give its address, here line 1, followed by the parameter "i" (to insert).
The "-i" option of "sed -i" is only there to write directly to the file without going through a temporary file.
--
Z'@+...che.JP : Zen, my Nuggets ! ;-) Knowledge is only good if it is shared.
-
cp monfichier.txt temp0001.tmp
echo hello > monfichier.txt
cat temp0001.tmp >>monfichier.txt
rm temp0001.tmp
Here is the final solution.
Bye
Guillaume -
sed '1s/^/message\n/' fichier
Je suis embêté pour mettre un message au début de chaque ligne parce que sed 'i'message' fichier écrit le message entre chaque ligne et non au début de chaque ligne :(-
Hello,
Read this first ;-))
For your case, based on what you said :[jp@MDK tmpfs]$ cat fich.txt it is : sed 1i"message" file I'm having a hard time putting a message at the beginning of each line because sed i"message" file writes the message between each line and not at the beginning of each line :( [jp@MDK tmpfs]$ sed 's/.*/Message &/' fich.txt Message it is : Message sed 1i"message" file Message Message I'm having a hard time putting a message at the beginning of each line Message because sed i"message" file writes the message Message between each line Message and not at the beginning of each line :( [jp@MDK tmpfs]$
;-))
--
Z'@+...che.JP : Zen, my Nuggets ! ;-) Knowledge is only good when it's shared.
-
-
Sorry, I responded in DOS (whereas you wanted BASH)
Well, I forgot Linux (I haven't touched it in 5 years)
But basically
ren -> to rename or move (mv??)
type -> cat
del -> to delete a file (unln, erase???)
--
To err is human, but a true disaster
can only be computer-related. -
You're welcome,
but if you can provide a functional solution for bash, that could help others later!
(it's for the future)
and if everything works, don't forget to close the post
@+
--
To err is human, but a true disaster
can only be computer-related. -
Can you provide the exact command you type, please.
Also, display the version of "sed" (sed -V).
--
Z'@+...che.JP: Zen, my Nuggets! ;-) Knowledge is only valuable if it is shared.
-
Hello,
To add a line at the beginning of a file:
cp thefile.txt temp
cat - temp <<< "Swing the cauldron at the bottom of the wooden box "> thefile.txt
...
...
...
rm temp
This is the method I use
Good luck! -
Doesn't a command exist to insert a line at the beginning of a file without using a temporary file?
-
Well apparently not. It's due to the file system..
Or we would have to go play directly with the i-nodes...
If anyone has a solution anyway.- The answer is:
sed -i 1i"first line" file.txt
And if you want to insert in the 8th line:
sed -i 8i"first line" file.txt
The -i option indicates that the file should be modified.
If you do not include it, the result is output to standard output (on the screen).
I suggest working with sed without the -i option to prepare your move and then when you are sure of yourself... finalize by adding the -i option.
-
-
-
Hello
You need to do this in 4 steps:
ren monfichier.txt temp0001.tmp echo hello >monfichier.txt type temp0001.tmp >>monfichier.txt del temp0001.tmp
If you don't understand the trick, just ask and I'll explain it to you.
--
To err is human, but a true disaster can only be computer-related. -
In fact, there is no BASH command to insert a line at the beginning. However, you can add one at the end.
What he did was copy the file. He created another one and added your line. Then he placed all the lines from your first file at the end.
Clear, right? lol
See you!
Guillaume