tags:

views:

3801

answers:

7

In my bash script I need to extract just the path from the given URL. For example, from the variable containing string:

http://login:[email protected]/one/more/dir/file.exe?a=sth&b=sth

I want to extract to some other variable only the:

/one/more/dir/file.exe

part. Of course login, password, filename and parameters are optional.

Since I am new to sed and awk I ask you for help. Please, advice me how to do it. Thank you!

A: 

This perl one-liner works for me on the command line, so could be added to your script.

echo 'http://login:[email protected]/one/more/dir/file.exe?a=sth&b=sth' | perl -n -e 'm{http://[^/]+(/[^?]+)};print $1'

Note that this assumes there will always be a '?' character at the end of the string you want to extract.

ire_and_curses
Unfortunatelly ? character at the end is not always present in URLs, so I can't assume that. Ghostdog74's answer seems to be better.
Arek
I'm afraid Ghostdog74's answer also relies on the '?'. Try removing the '?' character from the url in the echo statement in that answer and you'll see that the result is incorrect.
ire_and_curses
Hmmm, I tested both answers now and both seem to produce correct result for me :) <code>echo 'http://example.com/one/more/dir/file.exe' | perl -n -e 'm{http://[^/]+(/[^?]+)};print $1'</code>produce: <code>/one/more/dir/file.exe</code>So for me it's correct. Now I have to pass that result to bash variable and finish my script.
Arek
gsub will do nothing if there is no ?.
ghostdog74
+2  A: 

gawk

echo "http://login:[email protected]/one/more/dir/file.exe?a=sth&amp;b=sth" | awk -F"/" '
{
 $1=$2=$3=""
 gsub(/\?.*/,"",$NF)
 print substr($0,3)
}' OFS="/"

output

# ./test.sh
/one/more/dir/file.exe
ghostdog74
this assumes there are no slashes after the '?'
glenn jackman
+1  A: 

Best bet is to find a language that has a URL parsing library:

url="http://login:[email protected]/one/more/dir/file.exe?a=sth&amp;b=sth"
path=$( echo "$url" | ruby -ruri -e 'puts URI.parse(gets.chomp).path' )

or

path=$( echo "$url" | perl -MURI -le 'chomp($url = <>); print URI->new($url)->path' )
glenn jackman
It seems it is the most elegant way, I will use it.
Arek
+2  A: 

If you have a gawk:

$ echo 'http://login:[email protected]/one/more/dir/file.exe?a=sth&amp;b=sth' | \
  gawk '$0=gensub(/http:\/\/[^/]+(\/[^?]+)\?.*/,"\\1",1)'

or

$ echo 'http://login:[email protected]/one/more/dir/file.exe?a=sth&amp;b=sth' | \
  gawk -F'(http://[^/]+|?)' '$0=$2'

Gnu awk can use regular expression as field separators(FS).

Hirofumi Saito
A: 

This uses bash and cut as another way of doing this. It's ugly, but it works (at least for the example). Sometimes I like to use what I call cut sieves to whittle down the information that I am actually looking for.

Note: Performance wise, this may be a problem.

Given those caveats:

First let's echo the the line:

echo 'http://login:[email protected]/one/more/dir/file.exe?a=sth&amp;b=sth'

Which gives us:

http://login:[email protected]/one/more/dir/file.exe?a=sth&amp;b=sth

Then let's cut the line at the @ as a convenient way to strip out the http://login:password:

echo 'http://login:[email protected]/one/more/dir/file.exe?a=sth&amp;b=sth' | \
cut -d@ -f2

That give us this:

example.com/one/more/dir/file.exe?a=sth&b=sth

To get rid of the hostname, let's do another cut and use the / as the delimiter while asking cut to give us the second field and everything after (essentially, to the end of the line). It looks like this:

echo 'http://login:[email protected]/one/more/dir/file.exe?a=sth&amp;b=sth' | \
cut -d@ -f2 | \
cut -d/ -f2-

Which, in turn, results in:

one/more/dir/file.exe?a=sth&b=sth

And finally, we want to strip off all the parameters from the end. Again, we'll use cut and this time the ? as the delimiter and tell it to give us just the first field. That brings us to the end and looks like this:

echo 'http://login:[email protected]/one/more/dir/file.exe?a=sth&amp;b=sth' | \
cut -d@ -f2 | \
cut -d/ -f2- | \
cut -d? -f1

And the output is:

one/more/dir/file.exe

Just another way to do it and this approach is one way to whittle away that data you don't need in an interactive way to come up with something you do need.

If I wanted to stuff this into a variable in a script, I'd do something like this:

#!/bin/bash

url="http://login:[email protected]/one/more/dir/file.exe?a=sth&amp;b=sth"
file_path=$(echo ${url} | cut -d@ -f2 | cut -d/ -f2- | cut -d? -f1)
echo ${file_path}

Hope it helps.

Jim
+1  A: 

The Perl snippet is intriguing, and since Perl is present in most Linux distros, quite useful, but...It doesn't do the job completely. Specifically, there is a problem in translating the URL/URI format from UTF-8 into path Unicode. Let me give an example of the problem. The original URI may be:

file:///home/username/Music/Jean-Michel%20Jarre/M%C3%A9tamorphoses/01%20-%20Je%20me%20souviens.mp3

The corresponding path would be:

/home/username/Music/Jean-Michel Jarre/Métamorphoses/01 - Je me souviens.mp3

%20 became space, %C3%A9 became 'é'. Is there a Linux command, bash feature, or Perl script that can handle this transformation, or do I have to write a humongous series of sed substring substitutions? What about the reverse transformation, from path to URL/URI?

(Follow-up)

Looking at http://search.cpan.org/~gaas/URI-1.54/URI.pm, I first saw the as_iri method, but that was apparently missing from my Linux (or is not applicable, somehow). Turns out the solution is to replace the "->path" part with "->file". You can then break that further down using basename and dirname, etc. The solution is thus:

path=$( echo "$url" | perl -MURI -le 'chomp($url = <>); print URI->new($url)->file' )

Oddly, using "->dir" instead of "->file" does NOT extract the directory part: rather, it formats the URI so it can be used as an argument to mkdir and the like.

(Further follow-up)

Any reason why the line cannot be shortened to this?

path=$( echo "$url" | perl -MURI -le 'print URI->new(<>)->file' )

Daniel U. Thibault