Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > replace some words in a file with a perl script.

Reply
Thread Tools

replace some words in a file with a perl script.

 
 
Wang Penghui
Guest
Posts: n/a
 
      12-12-2004
Hi all:

I am a newbie with perl. I have met a pazzle now.
I have a file with thousands of lines. And which line has four fields.
They are separated by a "\t".
Such as:
================file==================
line1first line1second line1third line1fourth
line2first line2second line2third line2fourth
line3first line3second line3third line3fourth
........
Now i want to replace some words in the fourth field. While the first
three fields stay here as before.
I have writen a little code about it. Here is it:

open (ZH,"file") || die "could not open filename!"
@instead=split(/\t/,<ZH>);
close (ZH) || die "could not close filename!"
open (ZH,">file") || die "could not open filename!"
foreach (@instead) {
s/original/changed/g
print ZH $_;
};
close (ZH) || die "could not close filename!"

This script would replace all the words matched in each field. But it's
not what i want to get.
Anyone could pick me up?
Thanks in advance!

Wang Penghui
 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      12-12-2004
"Wang Penghui" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi all:
>
> I am a newbie with perl.
>


You also appear to be a usenet newbie. Don't multi-post!

Matt


 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      12-12-2004
Wang Penghui <(E-Mail Removed)> wrote:

> I am a newbie with perl. I have met a pazzle now.



Your Question is Asked Frequently.

How do I change one line in a file/
delete a line in a file/
insert a line in the middle of a file/
append to the beginning of a file?


> I have a file with thousands of lines.



But your code only reads ONE of those lines!



> @instead=split(/\t/,<ZH>);



This is the only input in your code, it reads one line.


> open (ZH,">file") || die "could not open filename!"



So this will stomp over the file contents with one line.


> foreach (@instead) {
> s/original/changed/g

^^
^^

Syntax error.

It is pretty rude of you to not post Real Perl Code.

Have you seen the Posting Guidelines that are posted here frequently?


> print ZH $_;
> };



> This script would replace all the words matched in each field.



Have you run that program?

Didn't you notice the one-line thing already?


> But it's
> not what i want to get.
> Anyone could pick me up?



If you want to s/// on only one element in @instead then don't
loop over ALL of the elements of @instead.

Replace the foreach with:

$instead[3] =~ s/original/changed/g;
print ZH $_ foreach @instead; # but don't you want to put the
# tab chars back in?

--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace stop words (remove words from a string) BerlinBrown Python 6 01-17-2008 02:37 PM
Words Words utab C++ 6 02-16-2006 07:00 PM
Non-noise words are incorrectly recognised as noise words. Peter Strĝiman ASP .Net 1 08-23-2005 01:26 PM
replace words with bold words Lasse Edsvik ASP General 9 10-07-2003 01:19 PM
Re: A little bit of help regarding my linked list program required. - "words.c" - "words.c" Richard Heathfield C Programming 7 10-05-2003 02:38 PM



Advertisments