Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 24 Jan 2000 08:08:26 +0100 (CET)
From:      Micke Josefsson <mj@isy.liu.se>
To:        Juan Kuuse <kuuse@quik.guate.com>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   RE: UNIX search n replace
Message-ID:  <XFMail.000124080826.mj@isy.liu.se>
In-Reply-To: <200001211709.JAA06271@www.geocrawler.com>

next in thread | previous in thread | raw e-mail | index | archive | help

On 21-Jan-00 Juan Kuuse 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?
> 
> Thanks in advance!
> 
> Juan Kuuse
> kuuse@quik.guate.com
> 
> Geocrawler.com - The Knowledge Archive
> 
> 
> To Unsubscribe: send mail to majordomo@FreeBSD.org
> with "unsubscribe freebsd-questions" in the body of the message

Use 'sed' like this:

sed -e 's/SEARCHFOR/REPLACEWITH/g' infile outfile

You can let infile[s] be produced by 'find .' if you like or have the file
names in a file...

A little sh-programming like this maybe:

$for i in `find . -name "blah?.txt"`; do
>echo sed -e 's/hello/HELLO/g' $i >$i.tmp
>echo mv -f $i.tmp $i
>done

The '>':s are sh's prompt when doing a multiline command. Preceeding each line
'that does something' with echo is useful when trying out the command. Just
remove them when you're confident the script does what it should.
(It might be a good idea to try it in a temporary directory first...)

Cheers,
/M

----------------------------------
Michael Josefsson, MSEE
mj@isy.liu.se

This message was sent by XFMail
running on FreeBSD 3.1 
----------------------------------


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?XFMail.000124080826.mj>