Solved

Searching for a specific header in a file

Posted on 2003-10-30
7
153 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

19 Experts available now in Live!

Get 1:1 Help Now