Add a line with sed

Solved
wapette21 Posted messages 68 Status Member -  
 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.
Configuration: Windows XP Firefox 3.0

19 answers

  1. bkcuf Posted messages 1 Status Member 3
     
    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
    +++
    3
  2. wapette21 Posted messages 68 Status Member 5
     
    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.
    0
  3. nadynadia Posted messages 43 Status Member
     
    toto is an example, isn't it?
    0
  4. wapette21 Posted messages 68 Status Member 5
     
    Yes, toto, it's good for an example, I still have the problem sed: command garbled
    I am on Solaris 9.

    Thank you.
    0
  5. jipicy Posted messages 40842 Registration date   Status Moderator Last intervention   4 898
     
    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.
    0
  6. wapette21 Posted messages 68 Status Member 5
     
    Always the same error message,

    I tried with i but the same, maybe I should use an awk?

    Thank you
    0
  7. lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
     
    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
    0
  8. wapette21 Posted messages 68 Status Member 5
     
    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
    0
  9. lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
     
    Oups, I added an extra quote ;-)
    perl -pi.orig -e 'print "But I feel like it's coming\n" if $. == 3' file.txt

    --
    106485010510997108
    0
  10. wapette21 Posted messages 68 Status Member 5
     
    Ç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
    0
  11. wapette21 Posted messages 68 Status Member 5
     
    Could someone have another idea or syntax for sed?

    Thank you
    0
  12. jipicy Posted messages 40842 Registration date   Status Moderator Last intervention   4 898
     
    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.
    0
  13. wapette21 Posted messages 68 Status Member 5
     
    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
    0
  14. jipicy Posted messages 40842 Registration date   Status Moderator Last intervention   4 898
     
    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.
    0
  15. wapette21 Posted messages 68 Status Member 5
     
    Hello,

    I tried everything but it doesn't break the line, I also tried the '' "" with no success.

    Thank you
    0
  16. wapette21 Posted messages 68 Status Member 5
     
    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.
    0
  17. jipicy Posted messages 40842 Registration date   Status Moderator Last intervention   4 898
     
    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.
    -1