Thread View: comp.unix.shell
3 messages
3 total messages
Started by bolega
Sat, 22 Aug 2009 18:11
sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?
Author: bolega
Date: Sat, 22 Aug 2009 18:11
Date: Sat, 22 Aug 2009 18:11
18 lines
1835 bytes
1835 bytes
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
Re: sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?
Author: w_a_x_man
Date: Sat, 22 Aug 2009 18:24
Date: Sat, 22 Aug 2009 18:24
21 lines
2012 bytes
2012 bytes
On Aug 22, 1:11 pm, bolega <gnuist...@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
Re: sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?
Author: Ed Morton
Date: Sat, 22 Aug 2009 23:02
Date: Sat, 22 Aug 2009 23:02
25 lines
2182 bytes
2182 bytes
On Aug 22, 1:11 pm, bolega <gnuist...@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.
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.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads