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 réponses
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
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 :
The "-i" option of "sed -i" is only there to write directly to the file without going through a temporary file.
--
Z'@+...che.
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
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 :
--
Z'@+...che.
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.
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.
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.
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!
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!
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.
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.
sed -i '1iSalut' essai.txt becomes: sed -i 1i'Salut' essai.txt
I know I'm bringing this up after several years, but it has just been useful to me,
Best regards
Thaaank youuu
echo "my text" >> my_file
that's it, kiss
In your case, you just need to learn how to read ;-(