[perl-python] 20050126 find replace strings in file

Discussion in 'Perl Misc' started by Xah Lee, Jan 26, 2005.

  1. Xah Lee

    Xah Lee Guest

    © # -*- coding: utf-8 -*-
    © # Python
    ©
    © import sys
    ©
    © nn = len(sys.argv)
    ©
    © if not nn==5:
    © print "error: %s search_text replace_text in_file out_file" %
    sys.argv[0]
    © else:
    © stext = sys.argv[1]
    © rtext = sys.argv[2]
    © input = open(sys.argv[3])
    © output = open(sys.argv[4],'w')
    ©
    © for s in input:
    © output.write(s.replace(stext,rtext))
    © output.close()
    © input.close()

    -------------------------
    save this code as find_replace.py
    run it like this:
    python find_replace.py findtext replacetext in_file out_file

    the sys.argv is from sys. sys.argv[0] is the program's name itself.

    note the idiom
    "for variable_name in file_object"

    note that since this code reads each
    line in turn, so huge file is of no-problemo

    the code is based from Python
    Cookbook of Alex Martelli & David
    Ascher, page 121

    in Python terminal, type help() then
    'FILES' and or 'sys'
    for reference.

    try to modify this file for your
    needs.
    --------------------------------------
    In perl, similar code can be achieved.
    the following code illustrates.

    if (scalar @ARGV != 4) {die "Wrong arg! Unix BNF: $0 <sstr> <rstr>
    <file id1> <file id2>\n"}
    $stext=$ARGV[0];
    $rtext=$ARGV[1];
    $infile = $ARGV[2];
    $outfile = $ARGV[3];
    open(F1, "<$infile") or die "Perl fucked up. Reason: $!";
    open(F2, ">$outfile") or die "Perl fucked up. Reason: $!";
    while ($line = <F1>) {
    chomp($line);
    $line =~ s/$stext/$rtext/g;
    print F2 "$line\n";
    }
    close(F1) or die "Perl fucked up. Reason: $!";
    close(F2) or die "Perl fucked up. Reason: $!";
    Xah

    http://xahlee.org/PageTwo_dir/more.html
    Xah Lee, Jan 26, 2005
    #1
    1. Advertising

  2. Xah Lee

    Haibao Tang Guest

    OK. But please don't die throwing that string, or this post will lose
    its educational purpose as it was meant to be.
    Haibao Tang, Jan 26, 2005
    #2
    1. Advertising

  3. Xah Lee

    Guest

    Re: 20050126 find replace strings in file

    Xah Lee wrote:
    > © # -*- coding: utf-8 -*-
    > © # Python
    > ©
    > © import sys
    > ©
    > © nn = len(sys.argv)
    > ©
    > © if not nn==5:
    > © print "error: %s search_text replace_text in_file out_file" %
    > sys.argv[0]
    > © else:
    > © stext = sys.argv[1]
    > © rtext = sys.argv[2]
    > © input = open(sys.argv[3])
    > © output = open(sys.argv[4],'w')


    I guess there is no way to check if the file opened fine? What if the
    filesystem or file is locked for this user/session. Pretty puny
    language if it cannot tell you that it cannot do what you tell it to.
    > ©
    > © for s in input:
    > © output.write(s.replace(stext,rtext))
    > © output.close()
    > © input.close()


    Same for the close. Is there no way check a good close?


    >
    > -------------------------
    > save this code as find_replace.py
    > run it like this:
    > python find_replace.py findtext replacetext in_file out_file
    >
    > the sys.argv is from sys. sys.argv[0] is the program's name itself.
    >
    > note the idiom
    > "for variable_name in file_object"
    >
    > note that since this code reads each
    > line in turn, so huge file is of no-problemo
    >
    > the code is based from Python
    > Cookbook of Alex Martelli & David
    > Ascher, page 121
    >
    > in Python terminal, type help() then
    > 'FILES' and or 'sys'
    > for reference.
    >
    > try to modify this file for your
    > needs.
    > --------------------------------------
    > In perl, similar code can be achieved.
    > the following code illustrates.
    >
    > if (scalar @ARGV != 4) {die "Wrong arg! Unix BNF: $0 <sstr> <rstr>
    > <file id1> <file id2>\n"}
    > $stext=$ARGV[0];
    > $rtext=$ARGV[1];
    > $infile = $ARGV[2];
    > $outfile = $ARGV[3];
    > open(F1, "<$infile") or die "Perl fucked up. Reason: $!";
    > open(F2, ">$outfile") or die "Perl fucked up. Reason: $!";


    In 7 years of perl programming, I have never seen an open error that
    had anything to do with perl processing. Normally, if I get an error,
    the file does not exist, or has permissions set so that the current
    user/session is not allowed to open the file.

    > while ($line = <F1>) {
    > chomp($line);
    > $line =~ s/$stext/$rtext/g;
    > print F2 "$line\n";
    > }
    > close(F1) or die "Perl fucked up. Reason: $!";
    > close(F2) or die "Perl fucked up. Reason: $!";


    Same here. Never seen Perl **** up on closing a file. Usually
    something in the OS or file system that does it.
    > Xah
    >
    > http://xahlee.org/PageTwo_dir/more.html
    , Jan 26, 2005
    #3
  4. Xah Lee

    Dan Perl Guest

    Re: 20050126 find replace strings in file

    <> wrote in message
    news:...
    > I guess there is no way to check if the file opened fine? What if the
    > filesystem or file is locked for this user/session. Pretty puny
    > language if it cannot tell you that it cannot do what you tell it to.
    > ..........
    > Same for the close. Is there no way check a good close?


    An exception (IOError) is raised when a file operation fails. The open()
    and and close() statements should be enclosed in a try-except statement.

    Please, take Xah Lee's postings with more than just a grain of salt. As a
    matter of fact, you are better off ignoring them than trying to learn from
    them. He is an egomaniac who is learning python and who thinks that he can
    already teach others. Personally I doubt he will ever learn python well
    enough to teach others. Just look at his English. He has a similar list
    with a-word-a-day just like the perl-python a-lesson-a-day and he seems
    indeed to know a lot of words. But his grammar is horrendous (there's a
    word for you, Xah Lee!). And according to his own website he's been living
    in North America for more than 15 years! English is a second language for
    me too but you won't see me writing something like "this groups is for
    Perlers who wants to learn Python".

    It may be unfair to pick on his English, but it is the best way I can make
    the point to someone who does not know python that Xah Lee does not know
    what he's talking about. I find Xah Lee annoying and I could just ignore
    him, but I am concerned that some people may actually take his statements
    seriously and learn from them. I can't imagine why or how, but there are
    actually 26 members in the perl-python Yahoo! group who have registered to
    get these bogus lessons sent to them daily!

    Dan
    Dan Perl, Jan 26, 2005
    #4
  5. Xah Lee

    Ala Qumsieh Guest

    Re: 20050126 find replace strings in file

    wrote:

    > Xah Lee wrote:
    >>close(F1) or die "Perl fucked up. Reason: $!";
    >>close(F2) or die "Perl fucked up. Reason: $!";

    >
    >
    > Same here. Never seen Perl **** up on closing a file. Usually
    > something in the OS or file system that does it.


    In this case, I'm pretty sure it's the user.

    --Ala
    Ala Qumsieh, Jan 26, 2005
    #5
  6. Xah Lee wrote:
    [...]
    > In perl, similar code can be achieved.
    > the following code illustrates.
    >
    > if (scalar @ARGV != 4)


    Why scalar()? The comparison already creates a scalar context, no need to
    enforce it twice.

    > {die "Wrong arg! Unix BNF: $0 <sstr> <rstr>
    > <file id1> <file id2>\n"}
    > $stext=$ARGV[0];
    > $rtext=$ARGV[1];
    > $infile = $ARGV[2];
    > $outfile = $ARGV[3];


    Ouch, how ugly. What's wrong with a simple
    my ($one, $two, $three, $four) = @ARGV;

    or the standard way using shift
    for ($one, $two, $three, $four) {
    $_ = shift;
    }

    > open(F1, "<$infile") or die "Perl fucked up. Reason: $!";
    > open(F2, ">$outfile") or die "Perl fucked up. Reason: $!";


    Really? Usually it's either the OS or the user (disk full, no write
    permissions, wrong file name, ...)

    > while ($line = <F1>) {


    Why $line? It doesn't serve any useful purpose here.

    > chomp($line);


    Why chomp()? It doesn't serve any useful purpose here.

    > $line =~ s/$stext/$rtext/g;


    If you would not have used $line above then you would not need the binding
    here.

    > print F2 "$line\n";


    If you would not have chomped the line above then you would not need to add
    the newline back here. A simpler
    print F2 $_;
    would have sufficed

    > }
    > close(F1) or die "Perl fucked up. Reason: $!";
    > close(F2) or die "Perl fucked up. Reason: $!";


    I find this highly unlikely. If at all then the OS failed to complete the
    close.

    jue
    Jürgen Exner, Jan 26, 2005
    #6
  7. To follow up on Jurgen Exner's critique, I present Xah Lee's version, and
    then my rewritten version.

    "Xah Lee" <> writes:
    > if (scalar @ARGV != 4) {die "Wrong arg! Unix BNF: $0 <sstr> <rstr>
    > <file id1> <file id2>\n"}
    > $stext=$ARGV[0];
    > $rtext=$ARGV[1];
    > $infile = $ARGV[2];
    > $outfile = $ARGV[3];
    > open(F1, "<$infile") or die "Perl fucked up. Reason: $!";
    > open(F2, ">$outfile") or die "Perl fucked up. Reason: $!";
    > while ($line = <F1>) {
    > chomp($line);
    > $line =~ s/$stext/$rtext/g;
    > print F2 "$line\n";
    > }
    > close(F1) or die "Perl fucked up. Reason: $!";
    > close(F2) or die "Perl fucked up. Reason: $!";


    #!/usr/bin/perl
    use warnings;
    use strict;

    if (@ARGV != 4) {
    die "Wrong arg! Unix BNF: $0 <sstr> <rstr> <file id1> <file id2>"
    }
    my ($stext, $rtext, $infile, $outfile) = @ARGV;

    open my $infh, '<', $infile
    or die "Error opening input file [$infile]: $!";
    open my $outfh, '>', $outfile
    or die "Error opening output file [$outfile]: $!";

    while(<$infh>) {
    s/$stext/$rtext/g;
    print { $outfh } $_;
    }
    close($infh) or die "Error closing input file [$infile]: $!";
    close($outfh) or die "Error closing output file [$outfile]: $!";

    My version takes up more lines, but I don't count whitespace--
    whitespace is not expensive, and when used properly adds greatly to
    the readability of your program.

    I've set followups to the only appropriate group for Mr. Lee's
    postings.

    -=Eric
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Schwartz, Jan 27, 2005
    #7
  8. Re: 20050126 find replace strings in file

    [ Followup set ]


    Dan Perl <> wrote:

    > I can't imagine why or how, but there are
    > actually 26 members in the perl-python Yahoo! group who have registered to
    > get these bogus lessons sent to them daily!



    There is one born every minute.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 27, 2005
    #8
  9. Re: 20050126 find replace strings in file

    >>>>> "TM" == Tad McClellan <> writes:

    TM> [ Followup set ]


    TM> Dan Perl <> wrote:

    >> I can't imagine why or how, but there are actually 26 members
    >> in the perl-python Yahoo! group who have registered to get
    >> these bogus lessons sent to them daily!


    TM> There is one born every minute.

    And when I poked at the list to see if I could figure out who was
    subscribing, it appeared to me that at least one of the subscribers
    was a comp.lang.lisp regular with such generosity of spirit or free
    time and pain tolerance as to go to Xah's playground and correct his
    errors *there*.

    The other 25 - eh. The optimist in me says I won't have to worry
    about them when competing for jobs; the pessimist in me knows I'll be
    hired to maintain their code but forbidden to improve it.

    Charlton


    --
    cwilbur at chromatico dot net
    cwilbur at mac dot com
    Charlton Wilbur, Jan 27, 2005
    #9
  10. Xah Lee

    Dave Cross Guest

    Re: 20050126 find replace strings in file

    On Thu, 27 Jan 2005 03:19:25 +0000, Charlton Wilbur wrote:

    >>>>>> "TM" == Tad McClellan <> writes:

    >
    > TM> [ Followup set ]
    >
    >
    > TM> Dan Perl <> wrote:
    >
    > >> I can't imagine why or how, but there are actually 26 members
    > >> in the perl-python Yahoo! group who have registered to get
    > >> these bogus lessons sent to them daily!

    >
    > TM> There is one born every minute.
    >
    > And when I poked at the list to see if I could figure out who was
    > subscribing, it appeared to me that at least one of the subscribers
    > was a comp.lang.lisp regular with such generosity of spirit or free
    > time and pain tolerance as to go to Xah's playground and correct his
    > errors *there*.


    Well, I tried. But after I posted my first bunch of corrections he
    made the list moderated so only he could post to it. So it does seem a bit
    pointless me staying one the list now.

    Dave...
    Dave Cross, Jan 30, 2005
    #10
    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. Xah Lee
    Replies:
    9
    Views:
    2,443
    alex23
    Jan 27, 2005
  2. Xah Lee
    Replies:
    1
    Views:
    1,381
    YYusenet
    Jan 31, 2005
  3. anonym
    Replies:
    1
    Views:
    1,018
    Knute Johnson
    Jan 15, 2009
  4. lucy
    Replies:
    7
    Views:
    184
    Joe Smith
    Sep 3, 2004
  5. Xah Lee
    Replies:
    0
    Views:
    344
    Xah Lee
    Jan 31, 2005
Loading...

Share This Page