using sed, writing Windows-compatible "new line"

Discussion in 'PHP' started by ycc, May 10, 2009.

  1. #1
    I usually keep a Linux server, but my webpage temporarily resides on GoDaddy hosting.

    I use sed through php. The files that come out do not get new lines under Windows.

    I can make sed recognize new lines and exchanged them for html (<br>) but how do I put real new-line-characters into the file?

    I have seen the following advice but I am not sure how to apply it under php. I get the quotes wrong somehow.

     sed "s/$/`echo -e \\\r`/"            # command line under ksh
     sed 's/$'"/`echo \\\r`/"             # command line under bash
     sed "s/$/`echo \\\r`/"               # command line under zsh
     sed 's/$/\r/'                        # gsed 3.02.80
    
    Code (markup):
    Please tell me how to change the line below to give a text-file with new lines readable under both Linux & Windows.

    <?php
    $output = shell_exec('sed -e \'s/$/<br>/\'  in.txt');
    echo $output;
    ?>
    
    Code (markup):
     
    ycc, May 10, 2009 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    use this code \r\n
     
    EricBruggema, May 10, 2009 IP
  3. ycc

    ycc Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for feedback, I tried the two following, however I still don't get any new lines in the output file.
    $output = shell_exec('sed -e \'s/$/\r\n/\'  in.txt');
    I also tried:
    $output = shell_exec('sed -e \'s/$/\\r\\n/\'  in.txt');
    Code (markup):
     
    ycc, May 10, 2009 IP
  4. ycc

    ycc Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I think the problem is not in sed.

    I think the problem is that since I used "echo" the browser will interpret the result as html and new-line will disappear.

    If I just run the output to a file and then link the browser to this text-file, it all comes out OK (with new-lines). If the browser receives a link to a textfile, it will interpret the new-line character as new-line (whereas in html it will appear as a blank).

    $output = shell_exec('sed -e \'s/foo/bar/g\' -e \'s/\[/\{/g\' -e \'s/\]/\}/g\' in.txt > out.txt');
    Code (markup):
    If I direct the browser to out.txt, the new-line-characters are OK.

    I think this thread is solved. Sorry for putting a misleading question from the beginning.
     
    ycc, May 10, 2009 IP
  5. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #5
    no problem, as you have solved your problem! (that's why you posted here... so everyone learn from your tiny mistake!)
     
    EricBruggema, May 11, 2009 IP
  6. ycc

    ycc Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Tanks for the positive approach Eric, that helps a lot.

    I just learnt from an Ubuntu-using friend that I can put the following at the start of the php-file to make the browser react as if it were reading a text-file:
    <? header('Content-type: text/plain'); ?>
    Code (markup):
    Echo and other php-output will be interpreted as text and new-lines will show as new-lines in the browser.
     
    ycc, May 11, 2009 IP