Link to home
Start Free TrialLog in
Avatar of sridhar_dvjs
sridhar_dvjs

asked on

URLs

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,
Avatar of Peewee
Peewee

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



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";
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
Avatar of ozo
@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
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of japhyRPI
japhyRPI

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial