LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-02-2003, 05:41 PM   #1
The Jesus
Member
 
Registered: Mar 2002
Location: Cleveland, Ohio
Distribution: Ubuntu, Gentoo, Fedora Core 5, FreeBSD
Posts: 50

Rep: Reputation: 15
Exclamation Perl Parsing filename


I have a directory filled with medical image files. They look like the following:

smith_13_503_de7.p
smith_13_502_de7.v
jones_104_503_de7.p
jones_104_502_de7.v
etc....

I need to parse that filename so that I just have:

smith_13
jones_104

What is the best way to do this in perl? I need something similar to "cut" in a shell script.

Thanks in advance.
 
Old 10-02-2003, 10:54 PM   #2
joesbox
Member
 
Registered: Feb 2003
Location: hampton va
Distribution: ubuntu
Posts: 502

Rep: Reputation: 30
i am not sure what you are trying to do. are you wanting to just look for a set of files or are you trying to rename them??
 
Old 10-03-2003, 09:25 AM   #3
The Jesus
Member
 
Registered: Mar 2002
Location: Cleveland, Ohio
Distribution: Ubuntu, Gentoo, Fedora Core 5, FreeBSD
Posts: 50

Original Poster
Rep: Reputation: 15
Here is the whole situation..... I have opened a directory and are to get the names of all the files in the folder. I need to parse the image names so that I can use the shortened image name for placement in a database. I don't need to shorten the actaul filename saved on the disk. I just need to shorten it for the sake of the database.


Hope that makes sense.

Thanks.
 
Old 10-03-2003, 03:10 PM   #4
joesbox
Member
 
Registered: Feb 2003
Location: hampton va
Distribution: ubuntu
Posts: 502

Rep: Reputation: 30
as far as i am thinking you will need a gui for something like this. give me some time and i ca try and whip something out at work. i should have it ready for you tomorrow.
 
Old 10-03-2003, 03:21 PM   #5
The Jesus
Member
 
Registered: Mar 2002
Location: Cleveland, Ohio
Distribution: Ubuntu, Gentoo, Fedora Core 5, FreeBSD
Posts: 50

Original Poster
Rep: Reputation: 15
Figured it out thanks for your help....

use strict;
my %fnames; # use a hash to eliminate duplicates
while (<DATA>) {
if (/^(\w+_\d+)_\d+\w+/) {
$fnames{$1} = 1;
}
}
foreach (keys %fnames) {
print "$_\n";
}

__DATA__
smith_13_503_de7.p
smith_13_502_de7.v
jones_104_503_de7.p
jones_104_502_de7.v
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
URL parsing program in perl ludeKing Programming 3 09-30-2010 03:58 PM
Perl/regexp help... - query string parsing... lowpro2k3 Programming 4 05-11-2005 05:18 PM
filename truncation Perl belmer2 Programming 1 04-24-2005 01:31 PM
Parsing HTML using Perl smaida Programming 2 05-29-2004 01:20 PM
parsing a logfile in perl ludeKing Programming 2 04-30-2004 03:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration