Solved

Insert 2 Empty Rows after every 2K records

Posted on 2010-11-10
4
407 Views
Last Modified: 2012-05-10
Hi,

I have a file with 300,000 unique records and for every 2000 records i want to insert 2 empty rows or new lines.

Thanks in advance.
0
Comment
Question by:faithless1
  • 2
4 Comments
 
LVL 8

Expert Comment

by:wolfgang_93
ID: 34109527
You could use the % modulus function.

Here's an example:

open(INPUT,'<inputfile');
open(OUTPUT,'>outputfile');
my $linenum = 0;

while ($line = <INPUT>) {
  $linenum ++;
  print OUTPUT $line;
  if (($linenum % 2000) == 0) {
    print OUTPUT "\n\n";
  }
}
exit;



Try something like this:

my $count=0;
while (...) {
  print
0
 

Expert Comment

by:tzepx
ID: 34109530
#!/usr/bin/perl

open (FILE, '<300kfile') or die $!;
open (TMP, '>>tmp') or die $!;
my $count = 0;

while(<FILE>){
  @words = split(/\W+/);
    foreach $word (@words) {
      print TMP $word,"\n";
      $count++;
      if ($count%2000==0) {print TMP "\n\n"};
    }
}
close FILE;
close TMP;
exit 0;
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 34109553
perl -i.bak -pe '$_.="\n\n" unless $.%2000' file
0
 

Expert Comment

by:tzepx
ID: 34109565
btw, you have 300k records separated by what? each record on a new line? one record after another separated by space, comma, tab etc?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question