Shell Newline

Solved
Hello,
I have a problem that I've been trying to solve since Friday without success. In fact, I'm writing a shell script, where I need to insert a line using sed, followed by a newline and then another line:
sed s/line_to_replace/line1\nline2/g file > file;
The problem is that the newline is not recognized, and I just get line1nline2.
I doubled the double slash, and I get the same thing; when I triple it, I get line1\nline2.
I should mention that I'm using Putty to connect to the server; unfortunately, I have no choice of tool.
Thank you for your help.

6 answers

  • Votes
  • Date
  • Descending date
  1. Contributor
    Hello,

    What operating system are you using on your server?
    What version of "sed"?

    Try with the ASCII code:

    sed 's/lign_remplacer/ligne1\x0Aligne2/g' fichier > fichier

    It's a ZERO and not a capital O...

    --
    Zen my nuggets ;-)
    Do something for the environment, close your windows and adopt a penguin.
    1. Always the same problem: it shows me ...line1x0Aline2...
      How can I know the version of the server and the sed command please?
      Thank you
      1. Contributor
        Do you at least know if the server is running GNU/Linux or UNIX?

        Try:

        lsb_release -a or cat /etc/{release,version,issue}


        For "sed":

        sed --version
    2. The command you gave me didn't work, I tried the oslevel command, it returned: 5.2.0.0.
      uname -a returned: AIX xxxxx 2 5 .....
      For sed, the command didn't work.
      1. Contributor
        Indeed, on AIX, "sed" can be rather temperamental ;-((

        Try it like this:

        sed 's/lign_remplacer/ligne1\<ENTREE> ligne2/g' file > file


        <ENTREE> should be executed, not taken literally, right? ;-))
    3. Could you please explain this entry story to me more?
      Thank you.
      1. Contributor
        After the backslash, you press Enter and continue the rest of the syntax on the next line.
    4. And what if I have a shell?
      1. Contributor
        You press <Enter> without the backslash before it ;-)
      2. Contributor
        Finally no, I may have said something stupid, you do the same with the anti-slash and the <Enter> ;-)
    5. It worked, but by adding another backslash: \\
      Thank you very much for this help, you saved my day! :-)