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

2009-08-22 Thread MRAB

John W. Krahn wrote:

bolega wrote:

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 ".


$ 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.



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)

--
http://mail.python.org/mailman/listinfo/python-list


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

2009-08-22 Thread John W. Krahn

bolega wrote:

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 ".


$ 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
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-08-22 Thread Ed Morton
On Aug 22, 1:11 pm, bolega  wrote:
> 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.

Why?

> 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 ".

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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2009-08-22 Thread Jan Kaliszewski

22-08-2009 o 20:11:32 bolega  wrote:


sed/awk/perl:

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


$ rm -rf /home/bolega ; python -c 'for i in xrange(1000): print "I will  
never crosspost senselessly."'


;~]

--
Jan Kaliszewski (zuo) 
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-08-22 Thread Tim Chase

bolega wrote:

sed/awk/perl:


Better to post in the "sed" or "perl" mailing lists rather than a
Python list.  I saw an awk solution flew by.


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

I really prefer a sed one liner.


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











--
http://mail.python.org/mailman/listinfo/python-list


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

2009-08-22 Thread w_a_x_man
On Aug 22, 1:11 pm, bolega  wrote:
> 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

awk 'BEGIN{FS=OFS="to "}{gsub(/ /,"_",$1);print}' myfile
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2009-08-22 Thread bolega
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



-- 
http://mail.python.org/mailman/listinfo/python-list