help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sed/awk/perl: How to replace all spaces each with an underscore that


From: Ed Morton
Subject: Re: sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?
Date: Sat, 22 Aug 2009 16:02:27 -0700 (PDT)
User-agent: G2/1.0

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.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]