Add a line with sed
Solved
wapette21
Posted messages
68
Status
Member
-
Eric -
Eric -
Hello,
I'm getting back to you because I'm very happy with my last position.
I'm still struggling with the sed command.
I want to add a line to a text file, but I want to be able to insert it after
the line of my choice.
The command I'm using: sed '${num_ligne}a! $addtext' fichier.txt
but without success.
Thank you for your feedback.
I'm getting back to you because I'm very happy with my last position.
I'm still struggling with the sed command.
I want to add a line to a text file, but I want to be able to insert it after
the line of my choice.
The command I'm using: sed '${num_ligne}a! $addtext' fichier.txt
but without success.
Thank you for your feedback.
Configuration: Windows XP Firefox 3.0
19 answers
-
Hello,
I know this post is old, but for those still looking for a solution, here is what I propose.
To insert(i), add(a), or exchange(c) a line with the sed command
ex: add to the end of the file
sed -e '$a\text you want ' file
ex: add after the line "add this text after line "2"
sed -e '2a\text you want ' file
Using the (i) option, your text is positioned on line 2 and shifts the rest of the text.
I hope this helps someone
+++ -
Hello!
I just found this, I hope you find the answer in there ^^!
Have a good day
http://www.commentcamarche.net/forum/affich 1982836 commande sed -
Thank you for your reply, but the provided solution does not work on my server,
sed '4i\ toto' -> sed: command garbled: 4i\ toto
I read the tutorial on the site, but the examples are not very clear to me.
Thank you. -
-
Yes, toto, it's good for an example, I still have the problem sed: command garbled
I am on Solaris 9.
Thank you. -
num=4; sed "$num a\ But I feel it's coming " file.txt
;-))
--
JP - Penguin Breeder -
Do your part for the environment, close your windows and adopt a penguin. -
Always the same error message,
I tried with i but the same, maybe I should use an awk?
Thank you -
Hello,
or with perl ;-) (I haven't tested it)perl -pi.orig -e 'print "But I feel that it's coming\n" if $. == 3"' file.txt
--
106485010510997108 -
So I don't know Perl, but I tried and it gave me:
String found where operator expected at -e line 1, at end of line
(Missing semicolon on previous line?)
Can't find string terminator '"' anywhere before EOF at -e line 1.
Thank you -
Oups, I added an extra quote ;-)
perl -pi.orig -e 'print "But I feel like it's coming\n" if $. == 3' file.txt
--
106485010510997108 -
Ça marche mais c'est pas stable, je m'explique quand je le lance telle que tu me la donner ca marche cependant quand je l'adapte a mon script en rajouter des variables il fait des doublons dans mon fichiers notamment pour écrire a la dernière ligne.
mon utilisation : variable remplacer.
perl -pi.tmp -e 'print "toto\n" if $. == 11' fichier.txt
fichier.txt
# Fichier de configuration contenant les jours non ouvrables de la société (Pentecôte,
# Fichier de configuration contenant les jours non ouvrables de la société (Pentecôte,
# Fichier de configuration contenant les jours non ouvrables de la société (Pentecôte,
# Fichier de configuration contenant les jours non ouvrables de la société (Pentecôte,
# jours fériés), les dates sont au format JJ/MM/AAAA.
# jours fériés), les dates sont au format JJ/MM/AAAA.
# jours fériés), les dates sont au format JJ/MM/AAAA. Apres adaptation
# jours fériés), les dates sont au format JJ/MM/AAAA.
14/07/2006
14/07/2006
Mais je sens que ça va venir -> test via la commande non adapter
Merci -
Could someone have another idea or syntax for sed?
Thank you -
num=4; sed "$num s/.*/&\nBut I feel like it's coming/" file.txt
;-))
--
JP - Penguin breeder -
Do a gesture for the environment, close your windows and adopt a penguin. -
I think we're not far off, the command works but doesn't insert a line break:
num=4; sed "$num s/.*/&\n But I feel like it's coming/" file.txt
# Configuration file containing the company's non-working days (Pentecost,
# public holidays), the dates are in DD/MM/YYYY format.
14/07/2006
17/07/2006n But I feel like it's coming
18/07/2006
15/08/2006
if we could insert a line break it would be perfect.
Thank you -
Rajoute un "anti-slash" :
sed "$num s/.*/&\\\n Mais je sens que ça va venir/"
;-))
--
JP - Éleveur de pingouins -
Faites un geste pour l'environnement, fermez vos fenêtres et adoptez un manchot. -
Hello,
I tried everything but it doesn't break the line, I also tried the '' "" with no success.
Thank you -
Hello,
too bad I settled for redirecting the text into my file, so I'm doing an insertion at the end of the file,
echo $text >> $mon_fichier
thank you all. -
-
Hello,
jp@MDK:~/tmpfs ssh$ cat fichier.txt Hello, I'm getting back to you because I'm very happy with my latest job. I still struggle with the sed command. I want to add a line in a text file, but I want to be able to insert it after the line of my choice. jp@MDK:~/tmpfs ssh$ num=4; sed "$num a\But I feel like it's coming" fichier.txt Hello, I'm getting back to you because I'm very happy with my latest job. I still struggle with the sed command. But I feel like it's coming I want to add a line in a text file, but I want to be able to insert it after the line of my choice. jp@MDK:~/tmpfs ssh$
;-))
--
JP - Penguin breeder -
Do your part for the environment, close your windows and adopt a penguin.