Scripting Question - Strip filename from URL leaving Domain and Path
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Scripting Question - Strip filename from URL leaving Domain and Path
I'm new to linux and scipting in general. I have a text file with several thousand URL's - 1 URL per line. I need to come up with a few simple commands that can parse the domain information and path from a URL while stripping the filename and extension.
Now personally I'd do this in python, because it is easier to follow in this example without having to learn about regular expressions (http://en.wikipedia.org/wiki/Regular_expressions).
You should also have python installed.
Code:
#!/usr/bin/env python
#-*-coding:utf-8 -*-
#
file = open( "/path/to/file", 'r' ).readlines()
for url in file:
url_tmp = ""
for part in url.split("/"):
url_tmp += "%s/" % part
# Do something here with 'url_tmp'
Simply put, we open in file and store it in an array, one line in the file per element.
Then we use split() on each url; which as it suggests splits further by the forward slash into another array (We're defining it on the fly, no need to name it) and then steps into every element in there, adds it to the string 'url_tmp' and adds a forward slash back in.
There are better ways, but this is a small and powerful and will be more than good enough
In your example, you have more than the domain name---that would be "http://blah.com"
Here are 2 example Regexes that match common URLs (Not tested--may need extended regex flags. Note also that the "/" needs to be escaped in some situations):
##In a way, this might be more robust, but it also matches all manner of invalid URLs. In your case, you might want to match everything that was intended to be a URL in the context.)
Using the second example, you can do the editing with SED:
Code:
sed 's%\(http://[^/]*\)[^ ]*%\1%g' filename > newfilename
This uses a backreference --- \(...\) ---to capture everything up to the first "/" after the http:// and then re-insert that in place of the larger expressiong (which goes up the the first space)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.