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

Re: [PBML] Separating data into different files

Expand Messages
  • Huaer XC
    ... #/usr/bin/perl -w my $file = file. ; open(FI, $file$1 ) or
    Message 1 of 5 , Aug 1, 2005
      ---------------------------------------------
      #/usr/bin/perl -w
      my $file = 'file.';
      open(FI,"< DATA") or die "cant open Date: $!";
      foreach (<FI>) {
      if (/^\d\d\.(\d\d)\..*/) {
      open(FO, ">> $file$1") or die "cant open output file $file$1: $!";
      print FO;
      }
      }
      close(FO);
      close(FI);
      -----------------------------------------
      I wrote one for you, it works fine with me.(BTW. I am also a newbie).
      so pls correct me if I did something wrong. thx..

      Huaer

      On 8/1/05, apsawant <apsawant@...> wrote:
      > Hi All,
      >
      > I am a beginner in perl.
      > I want to separate the contents of a file (shown below) into different
      > files.
      > The contents of the file DATA are:
      > ------------------------------------
      > 10.05.2005 10:00 - amit
      > 10.05.2005 10:10 - amit
      > 10.05.2005 10:20 - prakash
      > 10.05.2005 10:40 - amit
      > 10.05.2005 10:50 - amit
      > 11.05.2005 10:00 - prakash
      > 11.05.2005 10:10 - amit
      > 11.05.2005 10:20 - amit
      > 11.05.2005 10:40 - amit
      > 11.05.2005 10:50 - amit
      > 10.06.2005 10:00 - amit
      > 10.06.2005 10:10 - prakash
      > 10.06.2005 10:20 - amit
      > 10.06.2005 10:40 - amit
      > 10.06.2005 10:50 - amit
      > 11.06.2005 10:00 - amit
      > 11.06.2005 10:10 - amit
      > 12.06.2005 10:20 - prakash
      > 13.06.2005 10:40 - amit
      > 14.06.2005 10:50 - amit
      > 10.07.2005 10:00 - amit
      > 10.07.2005 10:10 - prakash
      > 10.07.2005 10:20 - amit
      > 10.07.2005 10:40 - amit
      > 10.07.2005 10:50 - amit
      > 11.07.2005 10:00 - amit
      > 11.07.2005 10:10 - prakash
      > 14.07.2005 10:20 - amit
      > 17.07.2005 10:40 - amit
      > 19.07.2005 10:50 - prakash
      > ------------------------------------
      >
      > The contents of the separate file should be as follows:
      >
      > file.05
      > --------
      > 10.05.2005 10:00 - amit
      > 10.05.2005 10:10 - amit
      > 10.05.2005 10:20 - prakash
      > 10.05.2005 10:40 - amit
      > 10.05.2005 10:50 - amit
      > 11.05.2005 10:00 - prakash
      > 11.05.2005 10:10 - amit
      > 11.05.2005 10:20 - amit
      > 11.05.2005 10:40 - amit
      > 11.05.2005 10:50 - amit
      >
      > file.06
      > --------
      > 10.06.2005 10:00 - amit
      > 10.06.2005 10:10 - prakash
      > 10.06.2005 10:20 - amit
      > 10.06.2005 10:40 - amit
      > 10.06.2005 10:50 - amit
      > 11.06.2005 10:00 - amit
      > 11.06.2005 10:10 - amit
      > 12.06.2005 10:20 - prakash
      > 13.06.2005 10:40 - amit
      > 14.06.2005 10:50 - amit
      >
      > file.06
      > --------
      > 10.07.2005 10:00 - amit
      > 10.07.2005 10:10 - prakash
      > 10.07.2005 10:20 - amit
      > 10.07.2005 10:40 - amit
      > 10.07.2005 10:50 - amit
      > 11.07.2005 10:00 - amit
      > 11.07.2005 10:10 - prakash
      > 14.07.2005 10:20 - amit
      > 17.07.2005 10:40 - amit
      > 19.07.2005 10:50 - prakash
      >
      >
      > Basically, I want to separate the contents of the file DATA containing
      > all the data into separate files such that it contains the data for
      > each month (05, 06, 07) and names the file name as file.month (in this
      > particular example, file.05, file.06, file.07).
      > Any help is highly appreciated.
      >
      > Thanks,
      > Amit.
      >
      >
      >
      >
      >
      > Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html
      > Yahoo! Groups Links
      >
      >
      >
      >
      >
      >
      >
      >
    • a_z0_9_blah
      ... different ... [...] ... containing ... this ... Hello Amit This is a faily simple problem. What have you tried so far? We will be eager help you when we
      Message 2 of 5 , Aug 2, 2005
        --- In perl-beginner@yahoogroups.com, "apsawant" <apsawant@y...>
        wrote:
        > Hi All,
        >
        > I am a beginner in perl.
        > I want to separate the contents of a file (shown below) into
        different
        > files.
        > The contents of the file DATA are:

        [...]

        > Basically, I want to separate the contents of the file DATA
        containing
        > all the data into separate files such that it contains the data for
        > each month (05, 06, 07) and names the file name as file.month (in
        this
        > particular example, file.05, file.06, file.07).
        > Any help is highly appreciated.
        >
        > Thanks,
        > Amit.

        Hello Amit

        This is a faily simple problem. What have you tried so far?
        We will be eager help you when we see you've made some effort to
        solve it yourself. :-)

        As a hint :-), it will likely contain nested while loops, loop
        labels and the redo function.

        See: http://perldoc.perl.org/perlsyn.html
        and scroll down to the Loop Control section.
        It will show you how to use labels.

        Also, to learn about the redo function, see:
        http://perldoc.perl.org/perlfunc.html
        and scroll down to the redo function.

        Good luck!

        Chris
      • a_z0_9_blah
        Hi Again You may also want to look at the substr function which will also be on the same perldoc web page as the one containing the redo function. Chris
        Message 3 of 5 , Aug 2, 2005
          Hi Again

          You may also want to look at the substr function which will also be
          on
          the same perldoc web page as the one containing the redo function.

          Chris
        • Prasanna Goupal
          Hi, Please use following code. ####################################################### #!/usr/bin/perl use strict; my $line; while($line = ) { my @array
          Message 4 of 5 , Aug 2, 2005
            Hi,

            Please use following code.

            #######################################################

            #!/usr/bin/perl

            use strict;
            my $line;

            while($line = <STDIN>)
            {
            my @array = split(" ",$line);
            my @digit = split(/\./,$array[0]);
            open(INFILE,">>file.$digit[1]") || die "Error in opening file";
            print INFILE $line;
            close(INFILE);
            }
            exit 0;

            #######################################################

            Run your program as perl file.pl < Inputfile.txt

            Regards,

            Prasanna

            ~


            apsawant <apsawant@...> wrote:
            Hi All,

            I am a beginner in perl.
            I want to separate the contents of a file (shown below) into different
            files.
            The contents of the file DATA are:
            ------------------------------------
            10.05.2005 10:00 - amit
            10.05.2005 10:10 - amit
            10.05.2005 10:20 - prakash
            10.05.2005 10:40 - amit
            10.05.2005 10:50 - amit
            11.05.2005 10:00 - prakash
            11.05.2005 10:10 - amit
            11.05.2005 10:20 - amit
            11.05.2005 10:40 - amit
            11.05.2005 10:50 - amit
            10.06.2005 10:00 - amit
            10.06.2005 10:10 - prakash
            10.06.2005 10:20 - amit
            10.06.2005 10:40 - amit
            10.06.2005 10:50 - amit
            11.06.2005 10:00 - amit
            11.06.2005 10:10 - amit
            12.06.2005 10:20 - prakash
            13.06.2005 10:40 - amit
            14.06.2005 10:50 - amit
            10.07.2005 10:00 - amit
            10.07.2005 10:10 - prakash
            10.07.2005 10:20 - amit
            10.07.2005 10:40 - amit
            10.07.2005 10:50 - amit
            11.07.2005 10:00 - amit
            11.07.2005 10:10 - prakash
            14.07.2005 10:20 - amit
            17.07.2005 10:40 - amit
            19.07.2005 10:50 - prakash
            ------------------------------------

            The contents of the separate file should be as follows:

            file.05
            --------
            10.05.2005 10:00 - amit
            10.05.2005 10:10 - amit
            10.05.2005 10:20 - prakash
            10.05.2005 10:40 - amit
            10.05.2005 10:50 - amit
            11.05.2005 10:00 - prakash
            11.05.2005 10:10 - amit
            11.05.2005 10:20 - amit
            11.05.2005 10:40 - amit
            11.05.2005 10:50 - amit

            file.06
            --------
            10.06.2005 10:00 - amit
            10.06.2005 10:10 - prakash
            10.06.2005 10:20 - amit
            10.06.2005 10:40 - amit
            10.06.2005 10:50 - amit
            11.06.2005 10:00 - amit
            11.06.2005 10:10 - amit
            12.06.2005 10:20 - prakash
            13.06.2005 10:40 - amit
            14.06.2005 10:50 - amit

            file.06
            --------
            10.07.2005 10:00 - amit
            10.07.2005 10:10 - prakash
            10.07.2005 10:20 - amit
            10.07.2005 10:40 - amit
            10.07.2005 10:50 - amit
            11.07.2005 10:00 - amit
            11.07.2005 10:10 - prakash
            14.07.2005 10:20 - amit
            17.07.2005 10:40 - amit
            19.07.2005 10:50 - prakash


            Basically, I want to separate the contents of the file DATA containing
            all the data into separate files such that it contains the data for
            each month (05, 06, 07) and names the file name as file.month (in this
            particular example, file.05, file.06, file.07).
            Any help is highly appreciated.

            Thanks,
            Amit.




            Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html



            ---------------------------------
            YAHOO! GROUPS LINKS


            Visit your group "perl-beginner" on the web.

            To unsubscribe from this group, send an email to:
            perl-beginner-unsubscribe@yahoogroups.com

            Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


            ---------------------------------




            ---------------------------------
            Start your day with Yahoo! - make it your home page

            [Non-text portions of this message have been removed]
          Your message has been successfully submitted and would be delivered to recipients shortly.
          »
          «