Link to home
Start Free TrialLog in
Avatar of Jam_Pa
Jam_Pa

asked on

Searching for a specific header in a file

Hi .. all  

I'm new to Perl, and I'm not sure how to tackle this type pf script.  

 I have 10 files in a  directory and each file has a different header, and I'd like to read each file and match each header from an array and then change the name of the file to a different name..

example

file1: header "RET100030  - reaname to file1.txt

I've created a korn schell do this , but I'd like to start using Perl.


thanks
James Parsons


ASKER CERTIFIED SOLUTION
Avatar of BioI
BioI
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or do you have an array with the different filenames in? and do you want to link the file 1 with the first element of your array that corresponds to a certain header?  Because in that case, I would suggest to use a hash...
This is the script when you want to rename filenames based on the header.  

#!/usr/bin/perl -w

use strict;
my $dir = "/path/to/your/directory";
opendir DIR, $dir;
my @files = readdir DIR;
my %hash = ('RET100030' => 'file1',
          'RET100040' => 'file2',
          'RET100050' => 'file3');
#hash with key the header (RET100030) and value the filename (file1)

for (my $i=0; $i<scalar(@files); $i++) {
    open FILE, "$files[$i]" || die "Unable to open file $files[$i] because $!\n";
    my $header = <FILE>;
    print "header --> ", $header, "\n";
    close FILE;
    my $command = "mv $files[$i] $hash{$header}";
    print "command --> ", $command, "\n";
    system $command;
}
SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kandura
kandura

why don't you show us your shell script? Then at least we know what you're doing now. Furthermore, it's not exactly clear what you're asking specifically. Please elaborate a bit.
Nothing has happened on this question in over 2 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points [grade B] between BioI [250 pts] and TinTin [250 pts] (abandoned, not clear what was asked).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer