Solved

URLs

Posted on 2002-03-05
7
183 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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 …
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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