LinuxQuestions.org
Review your favorite Linux distribution.
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 07-14-2002, 08:01 AM   #1
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
A simple perl programming question


I would like to tailor a perl script to my needs, for which I should only accomplish a very simple task:
I have a filename, stored in a variable, as follows:
$filename = "/path/to/file"
I would like to do a clean-up and delete that file, and also the directory in which it is located (/path/to).
I know how to delete the file:
unlink $filename
But how to delete the directory in which it was located?
First I should determine the directory name. I only have some knowledge in basic languages: there I would search the character position of the first occurence of "/" in the filename from the right, then use left$.
But how to do that in perl?
I have read the perlre and perlop man pages two times, but I still do not have the foggiest idea how to do this.
Please help me!
 
Old 07-14-2002, 12:21 PM   #2
FredrikN
Member
 
Registered: Nov 2001
Location: Sweden
Distribution: GNU/Linux since -97
Posts: 149

Rep: Reputation: 15
Hi.

Do you mean something like this:

#!/usr/bin/perl

# Set the $path and the $filename value right

$path = '/home/user/tmpdir/';
$filename = 'tmp.txt';


system ("rm $filename;rm -r $path");



or like this :


#!/usr/bin/perl

# Set the $path and the $filename value right

$path = '/home/user/tmpdir/';

system ("rm -r $path");
 
Old 07-14-2002, 01:29 PM   #3
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Original Poster
Rep: Reputation: 58
Yes, but first I should somehow determine the path, since it changes and it is not stored in a variable.
I suppose I could use a perl regular expression to cut the path out of the full filename. But how?

Last edited by J_Szucs; 07-14-2002 at 02:15 PM.
 
Old 07-14-2002, 02:19 PM   #4
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
$path =~ s/\/.*$//;
# $path now is /path/to (might wanna tack on the last /)

there's probably better ways, but that should work.
 
Old 07-14-2002, 02:46 PM   #5
FredrikN
Member
 
Registered: Nov 2001
Location: Sweden
Distribution: GNU/Linux since -97
Posts: 149

Rep: Reputation: 15
Did you try the '$path =~ s/\/.*$//;' ??

It removes the hole path .....

Give me a minute and I will try to fix it.....

Last edited by FredrikN; 07-14-2002 at 03:03 PM.
 
Old 07-14-2002, 02:49 PM   #6
FredrikN
Member
 
Registered: Nov 2001
Location: Sweden
Distribution: GNU/Linux since -97
Posts: 149

Rep: Reputation: 15
Okej, here we go, is this what you are searching for J_Szucs ??

#!/usr/bin/perl

$path = "/home/xxxx/tmp/test.txt";

print"\nPath to file : $path\n";

$path =~ s/\/(([^ ]|\n)*)\///g;

print "\nFilename : $path\n\n";



It will give you :



Path to file : /home/xxxx/tmp/test.txt

Filename : test.txt
 
Old 07-14-2002, 03:04 PM   #7
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
Quote:
Originally posted by FredrikN
Did you try the '$path =~ s/\/.*$//;' ??

It doesn't stripanything for me.
/home/xxxx/xxx/file.txt will remain the same.

Give me a minute and I will try to fix it.....
Of course not - I just said the first thing that poped into my head and assumed it was correct.
 
Old 07-14-2002, 03:05 PM   #8
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
By the way, FredrikN, it was good that you got the filename, but I think the path is what J_Szucs is looking for.
 
Old 07-14-2002, 03:10 PM   #9
FredrikN
Member
 
Registered: Nov 2001
Location: Sweden
Distribution: GNU/Linux since -97
Posts: 149

Rep: Reputation: 15
Ahh, okej, misstake by me , I thought it was file.

Will take a look at the path....

So the problem is the path /home/xxxx/xxx/
 
Old 07-14-2002, 03:11 PM   #10
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
I think my og expression dosen't work because of "greedy matching". I don't recall how to specify to do it only once. Maybe somebody can modify that?
 
Old 07-14-2002, 03:24 PM   #11
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
Long route (by using FredrickN's filename )
Code:
#!/usr/bin/perl

$path = "/home/xxxx/tmp/test.txt";

print"\nPath to file : $path\n";

$filename = $path;
$filename  =~ s/\/(([^ ]|\n)*)\///g;

print "\nFilename : $filename\n\n";

$path =~ s/$filename//;
print "PATH : $path\n";
 
Old 07-14-2002, 06:32 PM   #12
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Original Poster
Rep: Reputation: 58
Thanks to your help FredrikN and lackluster, now it works.
But what is your opinion, will that
system ("rm -r $path");
work on FreeBSD, too?
You know, I had some trouble nowadays when changing platforms...
 
Old 07-14-2002, 10:03 PM   #13
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
I've never used FreeBSD, but I imagine it wouldn't be a problem.
 
  


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
simple shall programming question itz2000 Programming 1 11-05-2005 04:12 PM
very simple bash programming question!? Thinking Programming 7 06-01-2005 11:07 AM
simple question on shell programming stupid_guy Programming 5 03-10-2004 02:07 PM
Simple C Programming Question.. Bolt Programming 6 06-03-2003 12:05 PM
Perl programming question LindsayJ Programming 0 11-11-2001 02:58 PM

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

All times are GMT -5. The time now is 10:17 AM.

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