Bash: inserting a line into a file

Johny jon -  
 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

11 réponses

informaticien.re
 
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
29
PSSGd1
 
In fact, his command is incorrect:
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
0
Lemez > PSSGd1
 

Thaaank youuu

0
eloodie
 
You're welcome!
0
hevoilajegalèrencor Posted messages 11 Registration date   Status Membre Last intervention  
 
Hi, I'm bringing the subject back up because you're overthinking the special and dedicated commands. An ECHO does the job:

echo "my text" >> my_file

that's it, kiss
0
zipe31 Posted messages 34620 Registration date   Status Contributeur Last intervention   6 501 > hevoilajegalèrencor Posted messages 11 Registration date   Status Membre Last intervention  
 
Hi,

In your case, you just need to learn how to read ;-(

I tried:
echo hello >> myfile.txt

but of course, it adds hello to the end of the file, whereas I want to add it right at the beginning of the file.
1
jipicy Posted messages 40842 Registration date   Status Modérateur Last intervention   4 898
 
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.
12
Johny jon
 
Hi,

so I tried the sed command like in your example, but the result I then have in my file is:

1isHello
Hello,
1isHello
test to see


It inserts 1isHello between each line. I'm trying to modify it, but if anyone has an answer, I'm also open to suggestions :)

Thanks everyone
0
chuchyyy
 
cp monfichier.txt temp0001.tmp
echo hello > monfichier.txt
cat temp0001.tmp >>monfichier.txt
rm temp0001.tmp

Here is the final solution.

Bye

Guillaume
4
tom
 

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 :(
2
jipicy Posted messages 40842 Registration date   Status Modérateur Last intervention   4 898
 
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.
0
sam3000 Posted messages 1226 Status Membre 144
 
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.
0
Johny jon
 
hi sam,

lol ok I see the trick. Complicated for such a small thing: finally with programming I ask myself fewer questions :)

Thanks again for your help :)
0
sam3000 Posted messages 1226 Status Membre 144
 
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.
-2
jipicy Posted messages 40842 Registration date   Status Modérateur Last intervention   4 898
 
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.
-2
Botojo Posted messages 9 Status Membre 51
 
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!
-2
toto
 
Doesn't a command exist to insert a line at the beginning of a file without using a temporary file?
-2
toto
 
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.
0
Gaston > toto
 
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.
0
frans
 
Simply:
echo -e "line to insert\n$(cat data)" > data
0
sam3000 Posted messages 1226 Status Membre 144
 
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.
-3
chuchyyy
 
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
-3