Re: [SLUG] String Replacement

From: Jason Copenhaver (jcopenha@csee.usf.edu)
Date: Wed Jul 04 2001 - 10:26:55 EDT


On Wed, 4 Jul 2001, tom ufer wrote:

> To: slug@nks.net
> Subject: String Replacement
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit
>
> This may be simple, but i'm reading and reading and I can't find out
> how. What i'm trying to do is a string replacement in many txt files at
> once. Say theres 100 files with the name Jim in them and I want to
> replace "Jim" with "Jim H". I can manually do it one by one with emacs,
> but there may be thousands of needed replacements and that would take
> OMG way too long.
>
> Any help?
>
> Thanks in advance.

You could write a perl script.. or awk.. or sed.. I've never used awk or
sed.. but you could do it in perl in only a couple of lines.. something
like

#!/usr/bin/perl

@files = glob("*.files.to.be.changed");
foreach $file (@files)
{
        open(INPUT,">$file");
        while(<INPUT>)
        {
                s/Jim/Jim H./g
        }
        close(INPUT)
}

something like that should work.. i'm coding perl off the top of my head
here though.. so.. it might not be exactly right..

Jason



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 15:29:28 EDT