Solved

Insert 2 Empty Rows after every 2K records

Posted on 2010-11-10
4
408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 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