Solved

URLs

Posted on 2002-03-05
7
182 Views
Last Modified: 2010-03-05
I have several URLs like
http://domain/dir1/dir4/../dir2/../../dir3/file.html
http://domain/dir1/dir2/../../dir3/file.html
http://domain/dir1/../dir3/file.html
etc..
How do I make them look like
http://domain/dir3/file.html (no ../../)
I guess a one liner can do the job!!
Thanks,
0
Comment
Question by:sridhar_dvjs
7 Comments
 
LVL 5

Expert Comment

by:Peewee
ID: 6841579
sridhar_dvjs,
i'm not fully sure what yo want here as your question looks short of information. Perhaps you could explain it a bit more.

However, it seems you would like to get rid of ../ within your URL's.  These characters tell the operating system to go to the preceeding directory.  If you take these out you will may navigate to the desired directory path.

if you wish to take them out you could use a regular expressions.

peewee



0
 
LVL 5

Expert Comment

by:Peewee
ID: 6841584
sridhar_dvjs,

an example regex is below:

#!/usr/bin/perl
print "start\n";
 
my $input = 'http://domain/dir1/dir4/../dir2/../../dir3/file.html';
 
#http://domain/dir1/dir2/../../dir3/file.html
#http://domain/dir1/../dir3/file.html
 
print "$input\n";
$input =~ s/\/\.\.//ig;
print "$input\n";
 
print "end\n";
0
 
LVL 2

Expert Comment

by:windfall
ID: 6841696
Something like this might work:

#!/usr/bin/perl
##########################################################################
@url = ('http://domain/dir1/dir4/../dir2/../../dir3/file.html',
'http://domain/dir1/dir2/../../dir3/file.html',
'http://domain/dir1/../dir3/file.html');

foreach $l(@url){
     
     $l =~ s/(\.\.\/)//gis;
print "$l\n";
}

Hope this helps..
windfall
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 84

Expert Comment

by:ozo
ID: 6841906
@url = ('http://domain/dir1/dir4/../dir2/../../dir3/file.html',
'http://domain/dir1/dir2/../../dir3/file.html',
'http://domain/dir1/../dir3/file.html');
foreach( @url ){
    1 while s![^/]+/\.\./!!;
    print "$_\n";
}
#assuming no symbolic links
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6842100
sridhar_dvjs,

"..I have several URLs like..."

Where are these URL's in? Are these URL's inside HTML or other types of files?

If yes, then can you post a sample file here?

"..I guess a one liner can do the job!!..."

Do you have command prompt access to run any command?

Please provide as much more detail as you can.

This will help you get a more accurate answer, faster.
0
 
LVL 1

Expert Comment

by:japhyRPI
ID: 6843129
What the poster is asking is "how can I change a directory path with ../'s in it to the path without ../'s?"  In other words, how can I get from

  /this/that/../foo/bar/blat/../it.txt

to

  /this/foo/bar/it.txt

Here is such code:

  $URL = "http://www.pobox.com/../~japhy/regexes/../

  # remove "leading" ../'s
  $URL =~ s{(http://[^/]+/)(?:\.\.(?:/|$))+}{$1};

  # remove other /..'s
  1 while $URL =~ s{[^/]+/\.\.(?:/|$)}{}g;

This code works with URLs such as "http://www.foo.bar/blat/..", even though the ".." doesn't have a trailing slash.
0
 
LVL 1

Accepted Solution

by:
japhyRPI earned 100 total points
ID: 6843137
Sorry, my line assigning to $URL is missing a closing quote and semicolon.

  $URL = "http://www.pobox.com/../~japhy/regexes/../";
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

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

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

18 Experts available now in Live!

Get 1:1 Help Now