unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?
@ 2009-08-22 18:11 bolega
  0 siblings, 0 replies; 3+ messages in thread
From: bolega @ 2009-08-22 18:11 UTC (permalink / raw)
  To: help-gnu-emacs

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





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?
       [not found] <1380cb14-6462-4585-b2d1-c59c99ff9b56@b15g2000yqd.googlegroups.com>
@ 2009-08-22 18:24 ` w_a_x_man
  2009-08-22 23:02 ` Ed Morton
  1 sibling, 0 replies; 3+ messages in thread
From: w_a_x_man @ 2009-08-22 18:24 UTC (permalink / raw)
  To: help-gnu-emacs

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?
       [not found] <1380cb14-6462-4585-b2d1-c59c99ff9b56@b15g2000yqd.googlegroups.com>
  2009-08-22 18:24 ` sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ? w_a_x_man
@ 2009-08-22 23:02 ` Ed Morton
  1 sibling, 0 replies; 3+ messages in thread
From: Ed Morton @ 2009-08-22 23:02 UTC (permalink / raw)
  To: help-gnu-emacs

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.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-08-22 23:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <1380cb14-6462-4585-b2d1-c59c99ff9b56@b15g2000yqd.googlegroups.com>
2009-08-22 18:24 ` sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ? w_a_x_man
2009-08-22 23:02 ` Ed Morton
2009-08-22 18:11 bolega

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).