Loading ...
Sorry, an error occurred while loading the content.

[PBML] Re: Perl: Processing multiple files from the commandline

Expand Messages
  • Jeff Boes
    ... Sure. See below. ... Now that you have the *.txt parameter, you just loop over it: while (glob($commandlineparams)) { &stripcr($_); } For more details,
    Message 1 of 7 , Nov 12, 1999
    • 0 Attachment
      --- Frank Hale <frankhale@...> wrote:
      > I have a script which strips the CRLF
      > from Windows text files. This makes them
      > look correct when viewing on Unix
      > systems. The script only handles one
      > file at a time. I want to make it handle
      > multiple files for instance if I pass it
      > the * (wildcard) I want it to process
      > all files in the given directory.
      >
      > Is there a quick and easy solution to
      > this?

      Sure. See below.

      > my @commandlineparms;
      >
      > &main;
      >
      > sub main {
      >
      > if ($ARGV[0]) {
      > $commandlineparms = $ARGV[0];


      Now that you have the '*.txt' parameter, you just loop
      over it:

      while (glob($commandlineparams)) {
      &stripcr($_);
      }

      For more details, see the Perl Cookbook.


      Note that this approach isn't precisely portable (at
      least, not at first glimpse). Unix will "pre-parse"
      the command line so that if you say:

      $ perl myscript.pl *.txt

      you won't see '*.txt' in the argument list @ARGV.



      =====
      Jeff Boes//ICQ=3394914//Yahoo!=jeffboes//AOL IM=jboes
      //home=jboes@...//professional=mur@...
      __________________________________________________
      Do You Yahoo!?
      Bid and sell for free at http://auctions.yahoo.com
    • Dan Boger
      you could just do the following: Replace : if ($ARGV[0]) { $commandlineparms = $ARGV[0]; &stripcr($commandlineparms); } ........ With: while
      Message 2 of 7 , Nov 12, 1999
      • 0 Attachment
        you could just do the following:

        Replace :

        if ($ARGV[0]) {
        $commandlineparms = $ARGV[0];

        &stripcr($commandlineparms);
        } ........

        With:

        while (defined($ARGV[0])) {
        $commandlineparms = shift;

        &stripcr($commandlineparms);
        } ........

        Untested code, but should work fine :)

        Cheers,

        Dan

        Dan Boger - Georgetown Institute for Cognitive and Computational Sciences
        dan@... ICQ: 1130750
        Georgetown University Medical Center Washington, DC
      • Frank Hale
        ... Well thank you for helping but your solution didn t work as expected. Here is the solution I ve come up with now: #!/usr/bin/perl # # Strips those nasty
        Message 3 of 7 , Nov 12, 1999
        • 0 Attachment
          --- Dan Boger <dan@...> wrote:
          > you could just do the following:
          >
          > Replace :
          >
          > if ($ARGV[0]) {
          > $commandlineparms = $ARGV[0];
          >
          > &stripcr($commandlineparms);
          > } ........
          >
          > With:
          >
          > while (defined($ARGV[0])) {
          > $commandlineparms = shift;
          >
          > &stripcr($commandlineparms);
          > } ........
          >
          > Untested code, but should work fine :)
          >

          Well thank you for helping but your solution didn't
          work as expected. Here is the solution I've come up
          with now:

          #!/usr/bin/perl
          #
          # Strips those nasty carraige returns off of windows
          text files.
          # useful when looking at those text files on a Unix
          system.
          #
          # Frank Hale
          # frankhale@...
          # 12 Nov 1999
          #
          # This script is released under the terms of the GNU
          public license
          # version 2 or later. Please refer to www.gnu.org for
          a copy of the license.
          ############################################################################
          my @commandlineparms;

          &main;

          sub main {

          for (@ARGV) {

          $commandlineparms = $_;

          print $commandlineparms."\n";

          &stripcr($commandlineparms);
          }

          if (not defined(@ARGV)) {
          print "\nstripcr - Strips the carraige returns from
          Windows text files.\n";
          print "By: Frank Hale\n";
          print "Date: 10 Oct 1999\n";
          print
          "--------------------------------------------------------------\n";
          print "usage: stripcr.pl [windows text file]\n";
          }
          }

          sub stripcr {

          my $file = $_[0];
          my @fileBuffer = ();

          open(FILE, "< $file") or die "Couldn't read from
          file: $!\n";
          @fileBuffer = <FILE>;
          close FILE;

          open(FIX_FILE, "> $file") or die "Couldn't read from
          file: $!\n";

          foreach $line (@fileBuffer)
          {
          $line =~ s/\r//g;
          print FIX_FILE $line;
          }

          close FIX_FILE;
          }


          Thanks!!!

          __________________________________________________
          Do You Yahoo!?
          Bid and sell for free at http://auctions.yahoo.com
        • Michael Hales
          Frank, How do I get off this listserve? It s way over my head. Thanks. ... From: Frank Hale [mailto:frankhale@yahoo.com] Sent: Friday, November 12, 1999
          Message 4 of 7 , Nov 15, 1999
          • 0 Attachment
            Frank,

            How do I get off this listserve? It's way over my head. Thanks.

            -----Original Message-----
            From: Frank Hale [mailto:frankhale@...]
            Sent: Friday, November 12, 1999 06:19 PM
            To: perl-beginner@egroups.com; tcg@...
            Subject: [PBML] Re: Perl: Processing multiple files from the commandline


            --- Dan Boger <dan@...> wrote:
            > you could just do the following:
            >
            > Replace :
            >
            > if ($ARGV[0]) {
            > $commandlineparms = $ARGV[0];
            >
            > &stripcr($commandlineparms);
            > } ........
            >
            > With:
            >
            > while (defined($ARGV[0])) {
            > $commandlineparms = shift;
            >
            > &stripcr($commandlineparms);
            > } ........
            >
            > Untested code, but should work fine :)
            >

            Well thank you for helping but your solution didn't
            work as expected. Here is the solution I've come up
            with now:

            #!/usr/bin/perl
            #
            # Strips those nasty carraige returns off of windows
            text files.
            # useful when looking at those text files on a Unix
            system.
            #
            # Frank Hale
            # frankhale@...
            # 12 Nov 1999
            #
            # This script is released under the terms of the GNU
            public license
            # version 2 or later. Please refer to www.gnu.org for
            a copy of the license.
            ############################################################################
            my @commandlineparms;

            &main;

            sub main {

            for (@ARGV) {

            $commandlineparms = $_;

            print $commandlineparms."\n";

            &stripcr($commandlineparms);
            }

            if (not defined(@ARGV)) {
            print "\nstripcr - Strips the carraige returns from
            Windows text files.\n";
            print "By: Frank Hale\n";
            print "Date: 10 Oct 1999\n";
            print
            "--------------------------------------------------------------\n";
            print "usage: stripcr.pl [windows text file]\n";
            }
            }

            sub stripcr {

            my $file = $_[0];
            my @fileBuffer = ();

            open(FILE, "< $file") or die "Couldn't read from
            file: $!\n";
            @fileBuffer = <FILE>;
            close FILE;

            open(FIX_FILE, "> $file") or die "Couldn't read from
            file: $!\n";

            foreach $line (@fileBuffer)
            {
            $line =~ s/\r//g;
            print FIX_FILE $line;
            }

            close FIX_FILE;
            }


            Thanks!!!

            __________________________________________________
            Do You Yahoo!?
            Bid and sell for free at http://auctions.yahoo.com

            ------------------------------------------------------------------------
            A shoppers dream come true! Find practically anything on earth at eBay!
            Come and browse the more than 2 million items up for bid at any time.
            You never know what you might find at eBay!
            http://clickhere.egroups.com/click/1140

            -- 20 megs of disk space in your eGroup's Document Vault
            -- http://www.egroups.com/docvault/perl-beginner/?m=1
          • Greg Webster
            ... Only if you let it...this IS the Perl-beginner mailing list, and as such you can ask any question whatsoever and 99% of the time get an answer you can
            Message 5 of 7 , Nov 15, 1999
            • 0 Attachment
              On Mon, 15 Nov 1999, you formulated thoughtfully:
              > Frank,
              >
              > How do I get off this listserve? It's way over my head. Thanks.

              Only if you let it...this IS the Perl-beginner mailing list, and as
              such you can ask any question whatsoever and 99% of the time get an
              answer you can use...

              However, the answers will depend on the question...a complicated
              question will recieve a complicated answer. A simple question
              recieves a simple answer.

              If you still wish to leave, you can do it on the egroups.com website.

              --
              Greg Webster
              http://www.geekrights.org
              Power to the precocious!
            • tlowery@dsioj.dla.mil
              Frank Hale: Here is another example of how to accomplish the process. You may want to look into renaming the input file because of memory. Slurping a whole
              Message 6 of 7 , Nov 16, 1999
              • 0 Attachment
                Frank Hale:
                Here is another example of how to accomplish the process. You may
                want to look into renaming the input file because of memory. Slurping
                a whole file could cause problems.


                #
                #
                #

                use strict;

                do { print <<EOP;
                stripcr - Strips the carraige returns from Windows text files.
                By: Frank Hale\n
                Date: 10 Oct 1999\n
                --------------------------------------------------------------
                usage: stripcr.pl [windows text file]
                EOP
                exit;
                } unless @ARGV;


                stripcr($_) for (@ARGV);

                exit;

                sub stripcr {
                my $file = shift;


                open(FILE, "< $file") or die qq{Couldn't read from file: $!\n}; #'

                # Slurping the whole file into memory?
                my @fileBuffer = <FILE>;
                close FILE;

                # Reopen the file, truncating it.
                open(FIX_FILE, "> $file") or die qq{Couldn't write from file: $!\n}; #'
                print FIX_FILE map { tr/\r//d; $_ } @fileBuffer;
                close FIX_FILE;
                }

                __END__
              Your message has been successfully submitted and would be delivered to recipients shortly.