Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Insert 2 Empty Rows after every 2K records

Posted on 2010-11-10
4
Medium Priority
?
410 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 …
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…
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…

670 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