sed regexp mystery

Discussion in 'Perl' started by John K. Humkey, Jul 8, 2003.

  1. Ok, it's sed not perl, but it's primarily a regexp question that I've
    encountered (and failed to understand) before. . .

    #
    # When trying to convert a variable number
    # of spaces separating fields, to fields
    # separated by single commas, if your "input"
    # file looks like the following lines. . .
    #
    #alpha bravo charlie delta echo fox
    #alpha bravo charlie delta echo fox
    #alpha bravo charlie delta echo fox
    #
    # and your're trying to convert it to the following. . .
    #
    #alpha,bravo,charlie,delta,echo,fox
    #alpha,bravo,charlie,delta,echo,fox
    #alpha,bravo,charlie,delta,echo,fox
    #
    # Why don't any of the next 5 lines work?
    cat input | sed -e "s/ +/,/g"
    cat input | sed -e "s/ */,/g"
    cat input | sed -e "s/ {1,10}/,/g"
    cat input | sed -e "s/[ ]*/,/g"
    cat input | sed -e "s/[ ]+/,/g"
    #
    # Because the above 5 lines all look like
    # valid methods of substituting one comma
    # for one or more spaces.
    #
    # And yet, the next line DOES work.
    cat input | sed -e "s/[ ] */,/g"
    #

    I've also tried the g/before/s//after/g type lines with even less success.

    Thoughts?

    jkh
     
    John K. Humkey, Jul 8, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. NNTP

    sed awk or perl for this?

    NNTP, Sep 11, 2003, in forum: Perl
    Replies:
    13
    Views:
    3,508
    Alan Connor
    Sep 30, 2003
  2. gorda
    Replies:
    2
    Views:
    556
    Andrew Shitov
    Oct 21, 2003
  3. NNTP
    Replies:
    2
    Views:
    966
    rakesh sharma
    Apr 7, 2004
  4. Steve Throckmorton

    mystery regexp

    Steve Throckmorton, Jul 17, 2006, in forum: Ruby
    Replies:
    4
    Views:
    121
    James Edward Gray II
    Jul 17, 2006
  5. Joao Silva
    Replies:
    16
    Views:
    402
    7stud --
    Aug 21, 2009
Loading...

Share This Page