Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 22 Jan 2000 09:23:54 +0530
From:      Greg Lehey <grog@lemis.com>
To:        John Russell <jr@paranoia.demon.nl>
Cc:        Charles Randall <crandall@matchlogic.com>, freebsd-questions@FreeBSD.ORG
Subject:   Re: UNIX search n replace
Message-ID:  <20000122092354.D455@mojave.worldwide.lemis.com>
In-Reply-To: <200001211848.TAA24196@gazelle.bigmama.xx>; from jr@paranoia.demon.nl on Fri, Jan 21, 2000 at 07:48:47PM %2B0100
References:  <64003B21ECCAD11185C500805F31EC0304D97722@houston.matchlogic.com> <200001211848.TAA24196@gazelle.bigmama.xx>

next in thread | previous in thread | raw e-mail | index | archive | help
On Friday, 21 January 2000 at 19:48:47 +0100, John Russell wrote:
>> On  Friday, January 21, 2000 10:10 AM, Juan Kuuse <archiver@db.geocrawler.com> wrote:
>>>
>>> This message was sent from Geocrawler.com by "Juan Kuuse"
>>> <kuuse@quik.guate.com>
>>> Be sure to reply to that address.
>>>
>>> More a common UNIX question than a FreeBSD topic:
>>>
>>> grep
>>> to search a keyword multiple files
>>>
>>> How do I search and replace one keyword for
>>> another in multiple files, any suggestions?
>>
>> Grab a Perl manual and build something along the lines of,
>>
>>> perl -i.bak -pe 's/word1/word2/g;'

Not quite the generic UNIX way.

> or use
> find . | sed s/word1/word2/g

This will change the file names, not their contents, and it won't save
it back.  You would probably also want to get into the habit of
writing quotes around the sed expression, in case it contains
delimiters.

A better approach to the original question would be:

  for i in *.c *.h; do 
    sed <$i 's/word1/word2/g' >$i.bak; 
    mv $i.bak $i; 
  done

*.c *.h represents the list of files you want to change.  You can use
any globbing pattern or list of globbing patterns here.  You can also
perform multiple changes in one pass: use the syntax

  's/word1/word2/g; s/word3/word4/g'

The 'g' at the end of the expressions means "change every occurrence
in the line".  If you omit it, you only change the first occurrence.

Greg
--
Finger grog@lemis.com for PGP public key
See complete headers for address and phone numbers


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20000122092354.D455>