[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

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


0
Jam_Pa
Asked:
Jam_Pa
2 Solutions
 
BioICommented:
I suppose you want to change the filename file1.txt to the header of the file RET100030.txt?


#!/usr/bin/perl -w

use strict;
my $dir = "/users/sista/pmonsieu/perl/expert/";
opendir DIR, $dir;
my @files = readdir DIR;

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] $header";
    print "command --> ", $command, "\n";
    system $command;
}
0
 
BioICommented:
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...
0
 
BioICommented:
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;
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TintinCommented:
It's a little unclear exactly what you need, so here's my intpretation.

#!/usr/bin/perl
use strict;

foreach my $file (<file*>) {
  open FILE, $file or die "Can not open $file $!\n";
  $_=<FILE>;
  m/(\S+)\s*$/;
  rename $file, $1 or warn "Can not rename $file to $1 $!\n";
}

0
 
kanduraCommented:
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.
0
 
jmcgOwnerCommented:
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now