sed/awk/perl: How to replace all spaces each with an underscore thatoccur before a specific string ?

Discussion in 'Python' started by bolega, Aug 22, 2009.

  1. bolega

    bolega Guest

    sed/awk/perl:

    How to replace all spaces each with an underscore that occur before a
    specific string ?

    I really prefer a sed one liner.

    Example
    Input : This is my book. It is too thick to read. The author gets
    little royalty but the publisher makes a lot.
    Output: This_is_my_book._It_is_too__thick_to read. The author gets
    little royalty but the publisher makes a lot.

    We replaced all the spaces with underscores before the first occurence
    of the string "to ".

    Thanks
    Gnuist
     
    bolega, Aug 22, 2009
    #1
    1. Advertisements

  2. bolega

    w_a_x_man Guest

    awk 'BEGIN{FS=OFS="to "}{gsub(/ /,"_",$1);print}' myfile
     
    w_a_x_man, Aug 22, 2009
    #2
    1. Advertisements

  3. bolega

    Tim Chase Guest

    Better to post in the "sed" or "perl" mailing lists rather than a
    Python list. I saw an awk solution flew by.
    Here's a one-liner sed solution:

    sed '/to /{s//\n&/;h;s/.*\n//;x;s/\n.*//;s/ /_/g;G;s/\n//}'

    There's a reason I prefer Python for these sorts of things:
    readability! You win 5 free internets (as a stand-in for the
    "real life" you clearly don't have) if you can decipher that in
    under 20 seconds ;-)

    expecting-to-see-NO CARRIER-after-typing-that'ly yers,

    -tkc
    +++ATH0
     
    Tim Chase, Aug 22, 2009
    #3
  4. $ rm -rf /home/bolega ; python -c 'for i in xrange(1000): print "I will
    never crosspost senselessly."'

    ;~]
     
    Jan Kaliszewski, Aug 22, 2009
    #4
  5. bolega

    Ed Morton Guest

    No, you replaced all ... the string "to " (note the space).

    awk '{idx=index($0,"to "); tgt=substr($0,1,idx-1); gsub(/ /,"_",tgt);
    print tgt substr($0,idx)}' file

    Ed.
     
    Ed Morton, Aug 23, 2009
    #5
  6. $ perl -le'
    $x = "This is my book. It is too thick to read. The author gets little
    royalty but the publisher makes a lot.";
    print $x;
    $x =~ /to / && substr( $x, 0, $-[0] ) =~ tr/ /_/;
    print $x;
    '
    This is my book. It is too thick to read. The author gets little
    royalty but the publisher makes a lot.
    This_is_my_book._It_is_too__thick_to read. The author gets little
    royalty but the publisher makes a lot.




    John
     
    John W. Krahn, Aug 23, 2009
    #6
  7. bolega

    MRAB Guest

    If you're interested in a Python regex solution:

    s = "This is my book. It is too thick to read. The author gets little
    royalty but the publisher makes a lot."
    s = re.sub(".*?(?=to )", lambda m: m.group().replace(" ", "_"), s)
     
    MRAB, Aug 23, 2009
    #7
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.