Solved

Searching for a specific header in a file

Posted on 2003-10-30
7
154 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Filtering a file to table 9 99
Perl Sort Question 4 129
Perl string replace for refred url 9 59
Perl script to delete older files 6 73
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

895 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

11 Experts available now in Live!

Get 1:1 Help Now