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

w_a_x_man w_a_x_man at yahoo.com
Sat Aug 22 20:24:56 CEST 2009


On Aug 22, 1:11 pm, bolega <gnuist... at gmail.com> 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



More information about the Python-list mailing list