Hi guys,

I have a several dozen .ini files from which I need to extract specific lines. The lines I'm pulling are repeated all throughout the code so I can't count on specific strings to pull these lines. My dilemma is that I want lines: 3, 4, 6, 47, 50, 60, 61, 63, ****and then 3, 4, 6 again*****, then 80, 83, 93, 94, 96. Unfortunately the code I have goes only in sequential order and I can't just enter lines 3, 4, 6 in between lines 63 and 80. Can anyone think of a way to do lines 3,4,6,47,50,60,61,63,3,4,6,80,83,93,94,96?

Thanks

use strict;
use warnings;
my $dir="c:\\input";
my $outputdir="c:\\input\\output";
my @files=`dir /b $dir\\C*.ini`;
my @lines=qw(3 4 6 47 50 60 61 63 80 83 93 94 96);
my %line;
for (@lines){
	$line{$_}=1;
}
my $count=0;
for (@files){
  s/\r//g;
  s/\n//g;
	open(FILE,"<","$dir\\$_");
	open(OUT,">","$outputdir\\$_");
	while(<FILE>){
	$count++;
	chomp;
	if($line{$count}){
		print OUT "$_\n";
	}
	}
	$count=0;
	close FILE;
	close OUT;
}

The only thing about this is that I think you need $line{$count-1} because if you're counting from 1, perl's arrays are counting from 0. Does that help at all? What output are you getting with the above code?

The current output is fine. It's giving me the lines I've got listed. The problem is I want lines 3, 4, and 6 again between lines 63 & 80. But it's going in order, and putting lines 3, 4, and 6 between lines 63 and 80 doesn't work.

So what I'm asking is if I've got the following text file:
line1
line2
line3
line4

I could edit my current code's line: my @lines=qw(1 2 3);
and my output would be:
line1
line2
line3

now what if i want lines 1, 2, 3, and then 2 again: my @lines=qw(1 2 3 2);
doesn't work. It'd give the same output above instead of listing what I desire which is:
line1
line2
line3
line2

Does this clarify?

It's hard for me to test without the files. Yet, here's what I would do. Open each file, read each file into an array (be sure to undef the arrays between files or let them go out-of-scope). For loop through the your @lines array and output the $file_array[$line_value]. Understand or should I go ahead and write some sample code with a couple of files?

I'm trying to understand but I'm too novice to understand how to execute what you're suggesting. Could you please provide some sample code if you have the time?
All this code is doing is:
reading all .ini files in c:\input
then outputting the same .ini files with only the lines I specified: 3 4 6 47 50 60 61 63 80 83 93 94 96 in c:\input\output.

If you could edit the code to do lines: 3 4 6 47 50 60 61 63 3 4 6 80 83 93 94 96

What I can do is show you something that works with two dummy ini files... Here is the code that works. Each ini file (C1.ini amd C2.ini) have the numbers 1-10 in each of the lines. Here's the code:

use strict;
use warnings;
my $dir=".\\input";
my $outputdir=".\\input\\output";
my @files=`dir /b $dir\\C*.ini`;
my @lines=qw(3 4 6 7 5 6 1 3 8 3 9 4 6);
my $file;
for $file (@files){
	print "$file\n";
  $file=~s/\r//g;
  $file=~s/\n//g;
	open(FILE,"<","$dir\\$file");
	my @line_array;
	my $z=0;
	while(<FILE>){
		chomp;
		$line_array[$z]=$_;
		$z++;
	}
	open(OUT,">","$outputdir\\$file");
	for (@lines){
		print OUT "$line_array[$_-1]\n";
	}
	
}

The output of each file is:

3
4
6
7
5
6
1
3
8
3
9
4
6

Just like you wanted. You probably will want to undef the @lines_array in there somewhere. (edit, sorry)

Thanks a lot for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.