From: Ed Morton <mortonspam@gmail.com>
To: help-gnu-emacs@gnu.org
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) [thread overview]
Message-ID: <cde6b9df-5acc-41a7-acb3-a0f37348d644__13181.0740022503$1251044911$gmane$org@z24g2000yqb.googlegroups.com> (raw)
In-Reply-To: 1380cb14-6462-4585-b2d1-c59c99ff9b56@b15g2000yqd.googlegroups.com
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.
next prev parent reply other threads:[~2009-08-22 23:02 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
[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 [this message]
2009-08-22 18:11 bolega
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='cde6b9df-5acc-41a7-acb3-a0f37348d644__13181.0740022503$1251044911$gmane$org@z24g2000yqb.googlegroups.com' \
--to=mortonspam@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.