replace some words in a file with a perl script.

Discussion in 'Perl Misc' started by Wang Penghui, Dec 12, 2004.

  1. Wang Penghui

    Wang Penghui Guest

    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
     
    Wang Penghui, Dec 12, 2004
    #1
    1. Advertising

  2. Wang Penghui

    Matt Garrish Guest

    "Wang Penghui" <> wrote in message
    news:...
    > Hi all:
    >
    > I am a newbie with perl.
    >


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

    Matt
     
    Matt Garrish, Dec 12, 2004
    #2
    1. Advertising

  3. Wang Penghui <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 12, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,861
  2. Peter Strøiman
    Replies:
    1
    Views:
    2,134
    Peter Strøiman
    Aug 23, 2005
  3. Richard Heathfield
    Replies:
    7
    Views:
    394
    Barry Schwarz
    Oct 5, 2003
  4. BerlinBrown
    Replies:
    6
    Views:
    4,834
  5. Lasse Edsvik

    replace words with bold words

    Lasse Edsvik, Oct 5, 2003, in forum: ASP General
    Replies:
    9
    Views:
    252
Loading...

Share This Page