Solved

Searching for a specific header in a file

Posted on 2003-10-30
7
155 Views
Last Modified: 2010-03-05
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
Comment
Question by:Jam_Pa
7 Comments
 
LVL 3

Accepted Solution

by:
BioI earned 250 total points
ID: 9650026
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
 
LVL 3

Expert Comment

by:BioI
ID: 9650053
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
 
LVL 3

Expert Comment

by:BioI
ID: 9650110
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 250 total points
ID: 9654105
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
 
LVL 18

Expert Comment

by:kandura
ID: 9659465
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
 
LVL 20

Expert Comment

by:jmcg
ID: 10038371
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now