I have numerous amounts of .ini files that I have to extract specific lines from. Say Lines 1,5,9,23,63,31 (each in a new line) from all files. So as you can see, no sort of pattern and I jump around in lines. I'd like to write a perl script (which I can set to run through all the .ini files in the folder. All file names begin with the prefix Dat , so Dat*.ini can be used and I would like the new . Is this even doable?

The reason I can't grep like my last post is, the lines I want are repeated all over the text file, and are sometimes just 2 characters long yielding many results =/

Thanks.

What are the lines? Are they the same line numbers in each file? Do they have anything in common? if they are the same line numbers, you could put the line numbers in a hash (set the value to 1 or something) and then extract the lines if the hash value is set.

Let me know if they are the same line numbers or have something else in common and I will create a script based on those parameters to do what you wish.

Mike

What are the lines? Are they the same line numbers in each file? Do they have anything in common? if they are the same line numbers, you could put the line numbers in a hash (set the value to 1 or something) and then extract the lines if the hash value is set.

Let me know if they are the same line numbers or have something else in common and I will create a script based on those parameters to do what you wish.

Mike

same line numbers everytime, here's what I've come up with thus far (I'm a total beginner by the way... learning the syntax slowly, and piecing it together). I'm sure you can tell though with my code below =)

use Tie::File;
use Fcntl "O_RDONLY";
tie my @line, "Tie::File", "example.ini", mode => O_RDONLY
    or die "Couldn't open file: $!";
print $line[3];
print "\n"; 
print $line[4];
print "\n"; 
print $line[6];
print "\n"; 
print $line[47];
print "\n"; 
print $line[50];
print "\n"; 
print $line[60];
print "\n"; 
print $line[61];
print "\n"; 
print $line[63];
print "\n"; 
print $line[80];
print "\n"; 
print $line[83];
print "\n"; 
print $line[93];
print "\n"; 
print $line[94];
print "\n"; 
print $line[96];

To answer your question Mike, yes it's always the same lines: 3,4,6,47,50,61,63,80,83,93,94,96.

A better clarification:

I have a folder: c:\data\ which contains:
Data1Class1.ini
Data1Class2.ini
Data2Class1.ini
Data2Class1.ini

I would like to extract the line numbers above from these files, and put them in c:\data\parsed\ with the same file names (or just replace the originals entirely with the rows desired, doesn't matter either way).

Edited 6 Years Ago by Grep: n/a

This should work on windows....

use strict;
use warnings;
my $dir="c:\\data";
my $outputdir="c:\\data\\parse";
my @files=`dir /b $dir\\Data*Class*.ini`;
my @lines=qw(3 4 6 47 50 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;
}
This article has been dead for over six months. Start a new discussion instead.