Solved

Insert 2 Empty Rows after every 2K records

Posted on 2010-11-10
4
404 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now