Professional Web Applications Themes

Convert line endings with sed - Mac Applications & Software

I'm puzzled.. Is there a way to convert mac line endings (\r) to unix line endings (\n) with sed? Steven -- nl...

  1. #1

    Default Convert line endings with sed

    I'm puzzled.. Is there a way to convert mac line endings (\r) to unix
    line endings (\n) with sed?

    Steven

    --
    nl


    Steven Guest

  2. #2

    Default Re: Convert line endings with sed

    Steven <nl> wrote in comp.sys.mac.system: 

    Sure. It's even simpler (and probably faster) with tr:

    tr "\r" "\n" <mac_file >unix_file

    Anno
    Anno Guest

  3. #3

    Default Re: Convert line endings with sed

    Anno Siegel wrote:

     
    >
    >
    > Sure. It's even simpler (and probably faster) with tr:
    >
    > tr "\r" "\n" <mac_file >unix_file[/ref]

    Thanks, but just aout of curiousity, how would it be done in sed? I've
    been puzzeling with for too long now and I need 'closure';)

    I found this:

    Unix

    Unix comes with many commands and filters that will do the conversions
    in any direction. Here are some "sed" examples:

    For DOS to Unix: sed s/.$// infilename >outfilename
    For Mac to Unix: sed s/x0d/x0a/ infilename >outfilename
    For Unix to DOS: sed s/$/x0d/ infilename >outfilename
    For Unix to Mac: sed s/x0a/x0d/ infilename >outfilename
    For DOS to Mac: sed s/x0d// infilename >outfilename
    For Mac to DOS: sed s/x0a/x0dx0a/ infilename >outfilename


    But it doesn't seem to work.

    Steven

    --
    nl


    Steven Guest

  4. #4

    Default Re: Convert line endings with sed

    Steven wrote:
     
    >>
    >>Sure. It's even simpler (and probably faster) with tr:
    >>
    >> tr "\r" "\n" <mac_file >unix_file[/ref]
    >
    > Thanks, but just aout of curiousity, how would it be done in sed? I've
    > been puzzeling with for too long now and I need 'closure';)
    >
    > I found this:
    >
    > Unix
    >
    > Unix comes with many commands and filters that will do the conversions
    > in any direction. Here are some "sed" examples:
    >
    > For DOS to Unix: sed s/.$// infilename >outfilename
    > For Mac to Unix: sed s/x0d/x0a/ infilename >outfilename
    > For Unix to DOS: sed s/$/x0d/ infilename >outfilename
    > For Unix to Mac: sed s/x0a/x0d/ infilename >outfilename
    > For DOS to Mac: sed s/x0d// infilename >outfilename
    > For Mac to DOS: sed s/x0a/x0dx0a/ infilename >outfilename
    >
    > But it doesn't seem to work.[/ref]

    BTW: the tr command you gave doesn't work either for me... I get:

    [sifre:~/Desktop] steven% tr "\r" "\n" myfile.txt > bla.txt
    usage: tr [-cs] string1 string2
    tr [-c] -d string1
    tr [-c] -s string1
    tr [-c] -ds string1 string2

    Steven


    --
    nl


    Steven Guest

  5. #5

    Default Re: Convert line endings with sed

    In article <supernews.com>,
    Steven <nl> wrote:
     
    > >
    > > Thanks, but just aout of curiousity, how would it be done in sed? I've
    > > been puzzeling with for too long now and I need 'closure';)
    > >
    > > I found this:
    > >
    > > Unix
    > >
    > > Unix comes with many commands and filters that will do the conversions
    > > in any direction. Here are some "sed" examples:
    > >
    > > For DOS to Unix: sed s/.$// infilename >outfilename
    > > For Mac to Unix: sed s/x0d/x0a/ infilename >outfilename
    > > For Unix to DOS: sed s/$/x0d/ infilename >outfilename
    > > For Unix to Mac: sed s/x0a/x0d/ infilename >outfilename
    > > For DOS to Mac: sed s/x0d// infilename >outfilename
    > > For Mac to DOS: sed s/x0a/x0dx0a/ infilename >outfilename
    > >
    > > But it doesn't seem to work.[/ref]
    >
    > BTW: the tr command you gave doesn't work either for me... I get:
    >
    > [sifre:~/Desktop] steven% tr "\r" "\n" myfile.txt > bla.txt
    > usage: tr [-cs] string1 string2
    > tr [-c] -d string1
    > tr [-c] -s string1
    > tr [-c] -ds string1 string2
    >[/ref]

    You're missing the input redirection. It should be:
    tr "\r" "\n" < myfile.txt > bla.txt
    ---

    --
    Tom Stiller

    PGP fingerprint = 5108 DDB2 9761 EDE5 E7E3
    7BDA 71ED 6496 99C0 C7CF
    Tom Guest

  6. #6

    Default Re: Convert line endings with sed

    Steven <nl> writes:
     

    Try using the man command for information on commands like tr and sed;
    it is quicker and more accurate than the advice you got on this
    newsgroup. The usage for tr is in the error message you quoted:

    tr "\r" "\n" < myfile.txt > bla.txt
    --

    Ronald Florence www.18james.com
    Ronald Guest

  7. #7

    Default Re: Convert line endings with sed

    Steven wrote:
     

    First likely reason is you're leaving off the input redirector, so
    you're trying to substitute a character that doesn't exist to be
    substituted. "sed" stands for "stream editor" and if you don't redirect
    input from a file using the < operator then sed will try your command on
    the string you give it and in the case of your example "infilename" has
    no line ending character, just 10 little alphabetics.

    Second likely reason is that you literally entered "x0d" and "x0a" in
    your sed stream. I've found that these hex numbers are treated as
    3-character sequences by sed so you need to use the escape mechanism, as
    in

    sed s/\r/\n/ < infilename > outfilename


    A note: sed is supposed to open the output file for writing before it
    starts processing the input, so if you try to "change in place" via

    sed s/string1/string2/ < file > file

    you'll end up with a zero-length nothin' in file.


    Jim
    --

    Longhaired freaky people need not apply.
    Jim Guest

  8. #8

    Default Re: Convert line endings with sed

    Tom Stiller wrote:
     

    Ooops, duh... Thanks,

    Steven

    --
    nl


    Steven Guest

  9. #9

    Default Re: Convert line endings with sed

    To all:

    Thanks for your postings on this thread.

    I've been here before, in particular, writing some "awk" scripts to process
    text files.

    The most important factor I learned is that

    "awk" won't work if its script file uses the wrong line endings!

    If you use Mac line endings, you can run "awk" all day, get no error
    messages, and it simply won't work.

    I'm not sure that anyone on this thread is actually using script files, but
    eventually manipulations get more complex and one ends up writing a sed or
    awk script in a file.

    In the real world, unfortunately, most of us need to deal with three line
    ending conventions.

    I keep a sticky on my desktop that reminds me:

    PC: \r\n 0x0d 0x0a
    Mac: \r 0x0d
    Unix: \n 0x0a

    Many programs take care of this for of us, but in the world of the MacOS X
    command line, we're on our own -- which may be exactly the right answer in
    the big picture.

    Unfortunately, we are more than ever at the mercy of commercial
    considerations with respect to our tools and conventions. I think it most
    likely that "product differentiation" and plain old competitiveness were at
    the heart of the divergence into these three conventions, and that the
    technical reasons for multiple standards were nil.

    We should be grateful, I suppose, that Apple didn't invent a fourth line
    ending standard for MacOS X.

    Thanks,

    Henry

    com remove 'zzz'

    Henry Guest

  10. #10

    Default Re: Convert line endings with sed

    Hi.

    com (Jim Hill) writes:
     

    This is not a property of sed, but of the Unix shell, and it's worth
    remembering. Issuing *any* command of the form
    foo < filename > filename
    will totally wipe out the contents of the file listed on the command
    line. It's an easy way to lose a lot of work, quickly.

    How can one protect oneself against this happening? Several shells
    (frinstance, tcsh, csh, bash) have a "noclobber" option. To be
    specific, let's suppose that your shell is tcsh. (If you're using
    another shell, read that shell's manpage for the details.) Simply put
    the line
    set noclobber
    in the .tcshrc file that's located in your home directory. Then any
    future shell processes that you launch will be protected against being
    overwritten by an output redirect. (If you want this protection in a
    currently-running shell process, type the command "set noclobber" in
    the terminal window.) If you ever *do* want to overwrite (or append)
    an existing file, simply use ">!" or ">>!", rather than ">" or ">>".

    For more info, do "man tcsh", "man csh", "man bash".

    --
    Art Werschulz (net)
    207 Stoughton Ave Cranford NJ 07016
    (908) 272-1146
    Art Guest

  11. #11

    Default Re: Convert line endings with sed

    Henry wrote:

     

    One silly thing that had me confused for some time was the fact that
    BBEdit was set to translate line endings on opening a doent. So when
    I used BBEdit to see of any of my attempts with sed and tr had worked it
    looked as if nothing had happend when in reality the command had worked. :-P

    Thanks all for helping me get on my feet with this. I'm trying to create
    a little script that appends the contents of daily logfiles into one
    file and strips the headers and blank lines. I want to let cron run it
    every day or so. This is what I have so far but I still have some single
    blank lines to get rid of:

    steven% cat log* | tr "\r" "\n" | sed -e 's/^Head.*//g' -e
    's/^Date.*//g' | cat -s > biglog.txt

    Steven


    --
    nl


    Steven Guest

  12. #12

    Default Re: Convert line endings with sed

    In article <BB6B8BEA.10CD7%com>,
    Henry <com> wrote:
     

    I've found the free program, BBEdit Lite, to be the most efficient way
    to inspect and/or convert text file line-end characters.

    [snip]

    --
    Tom Stiller

    PGP fingerprint = 5108 DDB2 9761 EDE5 E7E3
    7BDA 71ED 6496 99C0 C7CF
    Tom Guest

  13. #13

    Default Re: Convert line endings with sed

    Ronald Florence wrote:

     
    >
    > Stringing together handly little Unix programs like tr, sed, and cat
    > is the canonical solution to problems like this, and goes back to
    > Kernighan & Pike's _The Unix Programming Environment_. In the late
    > 1970s and early 1980s it was THE way to solve the problem. Today, you
    > might find that a single perl or awk script is simpler to write,
    > easier to maintain, and faster. Perl (CPAN) includes modules for many
    > housekeeping functions like log concatenation, trimming, and rotation.
    > For the relatively simple chores you've listed, you don't even need a
    > special module.
    >
    > The perl doentation is included with MacOSX.[/ref]

    You mean in the form of man pages? Or are you referring to some other
    form of doentation? I can't find any..

    Steven

    --
    nl


    Steven Guest

  14. #14

    Default Re: Convert line endings with sed

    Steven wrote:
     

    To dump blank lines, use:

    s/^$//


    Jim
    --

    Longhaired freaky people need not apply.
    Jim Guest

  15. #15

    Default Re: Convert line endings with sed

    Jim Hill wrote:
     
    >
    > To dump blank lines, use:
    >
    > s/^$//[/ref]

    Thanks, but doesn't that replace an empty line with an empty line?
    I used '/^$/d' which worked for me.

    Steven

    --
    nl


    Steven Guest

  16. #16

    Default Re: Convert line endings with sed

    Steven <nl> wrote in comp.sys.mac.system: 
    > >
    > > To dump blank lines, use:
    > >
    > > s/^$//[/ref]
    >
    > Thanks, but doesn't that replace an empty line with an empty line?[/ref]

    Right. You are also right to speak of empty lines, instead of blank
    lines, because that is what the pattern matches. For blank lines,
    something like /^[ \t]*$/ should be used.
     

    Quite so.

    Anno
    Anno Guest

  17. #17

    Default Re: Convert line endings with sed

    Steven wrote: 
    >
    >Thanks, but doesn't that replace an empty line with an empty line?[/ref]

    Yes, it does. I spaced out for a bit there.


    Jim
    --

    Longhaired freaky people need not apply.
    Jim Guest

Similar Threads

  1. How to convert from line chart data to CSV format
    By Venkat272808 in forum Macromedia Flex General Discussion
    Replies: 1
    Last Post: May 19th, 08:49 AM
  2. Help! My line endings change when I create a PDF . . . why?
    By Sara_Temby@adobeforums.com in forum Adobe Acrobat Macintosh
    Replies: 10
    Last Post: January 23rd, 07:56 AM
  3. Forced line break changes previous line endings
    By Olav_Kvern@adobeforums.com in forum Adobe Indesign Macintosh
    Replies: 12
    Last Post: September 8th, 01:11 AM
  4. Language change alters line endings
    By Kevin_R._Stauffer@adobeforums.com in forum Adobe Indesign Macintosh
    Replies: 5
    Last Post: July 6th, 06:41 PM
  5. I need to convert a string into an actual PHP line, how?
    By Phil Powell in forum PHP Development
    Replies: 0
    Last Post: September 3rd, 08:56 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139