Re: File contents

2002-05-07 Thread Stephane Bortzmeyer

On Mon, May 06, 2002 at 08:39:02PM +1000,
 Craig Sanders [EMAIL PROTECTED] wrote 
 a message of 32 lines which said:

 perl is the ideal tool to do this.

troll

No, you should use Python.

/troll
 


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: File contents

2002-05-07 Thread Stephane Bortzmeyer

On Tue, May 07, 2002 at 06:21:18PM +1000,
 Craig Sanders [EMAIL PROTECTED] wrote 
 a message of 27 lines which said:

 does python have an edit-in-place command line option, like perl's -i?

Unfortunately no, but you can easily write a script which will do the
same. Here is an example, quick and dirty, but doing a part of the job
of -i:

#!/usr/bin/python

import shutil
import sys
import re

file = sys.argv[1]
backup = file + '.bak'
pattern = re.compile (sys.argv[2])
replacement = sys.argv[3]

shutil.copyfile (file, backup)
backup_h = open (backup, r)
file_h = open (file, w)
line = backup_h.readline()

while line:
line = pattern.sub (replacement, line)
file_h.write (line)
line = backup_h.read()

file_h.close ()
backup_h.close ()


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: File contents

2002-05-07 Thread Martin WHEELER

On Tue, 7 May 2002, Stephane Bortzmeyer wrote:

  perl is the ideal tool to do this.

 troll

 No, you should use Python.

 /troll

Ah, but using which editor?  ;-)
-- 
Martin Wheeler [EMAIL PROTECTED] gpg key 01269BEB @ the.earth.li


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




File contents

2002-05-06 Thread Craig

Hi Fellows

Could someone help with changing file contents
in a specific directory with a number of files.

I used greg to extract the criteria but need
something to change it.

Thanks
..Craig :)


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: File contents

2002-05-06 Thread martin f krafft

also sprach Craig [EMAIL PROTECTED] [2002.05.06.1232 +0200]:
 Could someone help with changing file contents
 in a specific directory with a number of files.
 
 I used greg to extract the criteria but need
 something to change it.

man sed

-- 
martin;  (greetings from the heart of the sun.)
  \ echo mailto: !#^.*|tr * mailto:; net@madduck
  
the only real advantage to punk music is
that nobody can whistle it.



msg06305/pgp0.pgp
Description: PGP signature


Re: File contents

2002-05-06 Thread Craig Sanders

On Mon, May 06, 2002 at 12:32:17PM +0200, Craig wrote:
 Could someone help with changing file contents in a specific directory
 with a number of files.
 
 I used greg to extract the criteria but need something to change it.

perl is the ideal tool to do this.

in particular, see the perlrun man page for details about the -i option.

e.g.

perl -p -i.bak -e 's/foo/bar/g' *

will change all occurences of foo to bar in all files in the current
directory, first saving the original file(s) as .bak.

the perl script can be as simple or as complex as you need it to be.  it
is perl.

craig

-- 
craig sanders [EMAIL PROTECTED]

Fabricati Diem, PVNC.
 -- motto of the Ankh-Morpork City Watch


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: File contents

2002-05-06 Thread Ulf Rompe

Craig [EMAIL PROTECTED] writes:

 Could someone help with changing file contents in a specific
 directory with a number of files.

 I used greg to extract the criteria but need something to change it.

Greg should feel a bit abused... Poor guy. 

Or did you mean the program?

 greg - A tool testing framework.

Scary... ;-)

In addition to the other solutions mentioned in this list you can use
rpl:

 rpl is a UN*X text replacement utility. It will replace strings with
 new strings in multiple text files. It can work recursively over
 directories and supports limiting the search to specific file
 suffixes.

It's syntax is as simple as rpl oldstring newstring * but there are
some advanced options available.


Or you can use replace from mysql-server (OK, it is overkill to
install mysql just for getting this tool but maybe you use it anyway).

From the man page:

   replace - A utility program that is used by msql2mysql, but
   that has more general applicability as well. replace changes
   strings in place in files or on the standard input. Uses a
   finite state machine to match longer strings first. Can be used
   to swap strings.

[x] ulf

-- 
Hi! I'm a .signature virus! Copy me into your ~/.signature to help me spread!


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]