sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?
John W. Krahn
someone at example.com
Sat Aug 22 20:14:29 EDT 2009
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
More information about the Python-list
mailing list