Solved

Insert 2 Empty Rows after every 2K records

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what are these modules in perl script 4 99
listing unique file prefixes in a directory Linux 1 58
AWS CLI issues with Tags 3 77
Powershell Timeout - Exchange 3 46
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…

809 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