From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ed Morton Newsgroups: gmane.emacs.help 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) Organization: http://groups.google.com Message-ID: References: <1380cb14-6462-4585-b2d1-c59c99ff9b56@b15g2000yqd.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1251044911 28285 80.91.229.12 (23 Aug 2009 16:28:31 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 23 Aug 2009 16:28:31 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Aug 23 18:28:24 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1MfFvc-0002eS-3j for geh-help-gnu-emacs@m.gmane.org; Sun, 23 Aug 2009 18:28:24 +0200 Original-Received: from localhost ([127.0.0.1]:34116 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MfFvb-0007FH-Ad for geh-help-gnu-emacs@m.gmane.org; Sun, 23 Aug 2009 12:28:23 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!postnews.google.com!z24g2000yqb.googlegroups.com!not-for-mail Original-Newsgroups: comp.unix.shell, gnu.emacs.help, comp.lang.python, comp.lang.lisp, comp.lang.scheme Original-Lines: 25 Original-NNTP-Posting-Host: 67.173.128.90 Original-X-Trace: posting.google.com 1250982148 32572 127.0.0.1 (22 Aug 2009 23:02:28 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Sat, 22 Aug 2009 23:02:28 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: z24g2000yqb.googlegroups.com; posting-host=67.173.128.90; posting-account=rv7-7goAAAAyBCsUgaHaGs9haWRFGhT0 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729), gzip(gfe), gzip(gfe) Original-Xref: news.stanford.edu comp.unix.shell:229317 gnu.emacs.help:172284 comp.lang.python:606059 comp.lang.lisp:274227 comp.lang.scheme:82285 X-Mailman-Approved-At: Sun, 23 Aug 2009 12:12:46 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:67447 Archived-At: On Aug 22, 1:11=A0pm, bolega 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 : =A0This is my book. It is too =A0thick 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=3Dindex($0,"to "); tgt=3Dsubstr($0,1,idx-1); gsub(/ /,"_",tgt); print tgt substr($0,idx)}' file Ed.