Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting values from various files

Status
Not open for further replies.

kbshaq1

Technical User
Jan 20, 2010
3
US
Hello,

I have several text files containing identical information, and want to extract values from a certain field. Each of these values will go to a separate array (or hash) to be later placed in a CSV file.
For example, all "admin" values will go to an array or hash called "array" containing all admin values from various files.

file:
Code:
admin 28224
config 185920
jobs 3654972619793
replicated 691299178547

What's the best way to do this?

Thanks,
KBS
 
How about like this:
Code:
my %hash;
my @data = ("admin 28224", "config 185920", "jobs 3654972619793", "replicated 691299178547", "admin 12345");

for my $line (@data) {
	my @tmp =(split /\s+/, $line);
	push @{$hash{$tmp[0]}}, $tmp[1];
}


#Print out all admin's
for my $value (@{$hash{admin}}) {
	print "$value\n";
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Thanks, travs69.
I think this will work, but *how* do I get all the data (from all the files) into "@mydata" (array)?? That was one of my initial issues for this task.

Thanks again!
KBS
 
I don't think I understand what you are wanting, I thought you wanted each of the individual values in their own arrays not a full array containing every line of the file..



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Thanks again, travs69. Your script's output is what I needed, but I want the various files that contains the data (ie, admin, replicated, etc.) to be extracted by the script itself. How can I include that in your script?

Here is a sample of 2 of several files:

file1:
Code:
admin 28224
config 185920
jobs 3654972619793
replicated 691299178547

file2:
Code:
admin 123893
config 8128
replicated 781299178436
jobs 2654988919793

Thanks again,
KBS
 
If you show us what the output CSV file should look like we may understand your requirements better.

Annihilannic.
 
you should be able to find lot of examples to read/open files in perl. I don't mind helping you through part of it but you should be able to do the easy parts.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I'd probably just do it in a
Code:
while (<>) {
  ..
}
loop and use the shell to glob the files
Code:
perl myscript.pl file*.dat
when you invoke it...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Code:
my $path = '/path/to/dir';
my @files = ('file1.ext', 'file2.ext');
my %hash;

foreach (@files) {
	open (FILE, "<$path/$_") || die "Cannot Open $!";
	while (<FILE>) {
		my @tmp =(split /\s+/, $_);
		push @{$hash{$tmp[0]}}, $tmp[1];
	}
	close (FILE) || die "Cannot Close $!";
}

for my $value (@{$hash{admin}}) {
	print "$value\n";
}

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top