in reply to Splitting a Blocked file in Round Robin into smaller files

Are you saying that you want all the records starting with '1' in one file. All those starting with '2' in a second file. And all those starting with '3' in a third. And so on?

If not, you'll need to clarify your explanation because it is very confused.

Eg. What does "I need to split this large file into smaller 4 files by doing a round robin of each block(block 1 to 5)" mean?

Did you typo? Should that be "5 smaller files"?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re: Splitting a Blocked file in Round Robin into smaller files

Replies are listed 'Best First'.
Re^2: Splitting a Blocked file in Round Robin into smaller files
by tradersjoe0 (Novice) on Dec 14, 2015 at 15:57 UTC

    I am sorry for the confusion. Lets say the actual file has 5 blocks as shown below(each end of block is distinguished by a record having its first character as '5'). Now, I will be dividing this actual file into 4 smaller files

    Step 1: Go through the file, find the first record having the first character as '5' and copy until then to first file.

    Step 2:Copy the next block until you get first character as '5' into second file and so on until the entire file is divided into 4 smaller files in round robin fashion

    Actual File:
    1this is block 1 2this is block 1 3this is block 1 4this is block 1 2this is block 1 3this is block 1 5this is block 1 1this is block 2 2this is block 2 3this is block 2 2this is block 2 3this is block 2 5this is block 2 1this is block 3 2this is block 3 5this is block 3 1this is block 4 2this is block 4 5this is block 4 1this is block 5 3this is block 5 5this is block 5
    File1:
    1this is block 1 2this is block 1 3this is block 1 4this is block 1 2this is block 1 3this is block 1 5this is block 1 1this is block 5 3this is block 5 5this is block 5
    File 2:
    1this is block 2 2this is block 2 3this is block 2 2this is block 2 3this is block 2 5this is block 2
    File 3:
    1this is block 3 2this is block 3 5this is block 3
    File 4:
    1this is block 4 2this is block 4 5this is block 4

      Much better explanation, thank you. Try this:

      #! perl -sw use strict; my $file = $ARGV[0]; open I, '<', $file or die $!; my @outs; open $outs[ $_ ], '>', "$file.$_" or die $! for 1 .. 4; my $out = 1; while( <I> ) { print { $outs[ $out ] } $_; if( /^5/ ) { ++$out; $out = 1 if $out > 4; } }

      Call it as scriptname filename. The 4 output files will be named filename.1 filename.2 filename.3 filename.4


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Worked like a champ!!! Thank you so much for taking time...Really appreciate your help

        Hello BrowserUK,

        The above code works like a champ The 4 output files are named filename.1 filename.2 filename.3 filename.4

        Lets say filename= DummyFile.txt

        4 output files:DummyFile.txt.1,DummyFile.txt.2,DummyFile.txt.3,DummyFile.txt.4

        I am trying to get the file names as below

        4 output files:DummyFile1.txt,DummyFile2.txt,DummyFile3.txt,DummyFile4.txt

        The number comes before the delimiter "."

        Any help will be much appreciated