LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 03-08-2010, 11:37 AM   #1
adam1mc
LQ Newbie
 
Registered: Mar 2010
Posts: 2

Rep: Reputation: 1
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.

For example, if I have http://blah.com/123/abc.exe I want to strip the abc.exe and just leave http://blah.com/123/

Can anyone please help me out with this?

Thanks

Last edited by adam1mc; 03-08-2010 at 06:25 PM. Reason: Better title for future Googlers
 
Old 03-08-2010, 11:50 AM   #2
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 70
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
 
Old 03-08-2010, 12:03 PM   #3
adam1mc
LQ Newbie
 
Registered: Mar 2010
Posts: 2

Original Poster
Rep: Reputation: 1
That could work too. I'm just trying to learn the basics without getting too complex. :-)

Here is what I ended up using that worked well:

rev urllist | cut -d/ -f2- | rev > newurllist
 
1 members found this post helpful.
Old 03-08-2010, 12:14 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Another perspective: Learn Regular Expressions

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):
Code:
1.  http://([^\.]/.)*(com|net|org|edu|mil)

2.  http://[^/]*
##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)

Again---none of this tested. For a good reference on SED an other things, go here:
http://www.grymoire.com/Unix/
 
Old 03-08-2010, 12:16 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Hmmm---perhaps OP has a better answer---and in less time. Funny how that happens.....
 
Old 03-08-2010, 12:29 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
while read
do
    echo ${REPLY%/*}
done < input.txt
 
Old 03-08-2010, 12:32 PM   #7
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
My suggestion would have been along the lines of--

Code:
for i in $(cat filename.txt); do echo $i | cut -f1-$(echo $i | awk -F\/ '{print NF-1}') -d\/; done
Which isn't an elegant solution. However, I forgot about rev and it would enable you to simplify it to a single cut as the op did. Good call!

I don't often use rev and often lament that cut doesn't support working backwards on a line... /facepalm.

Good solution OP

Last edited by rweaver; 03-08-2010 at 12:34 PM.
 
Old 03-08-2010, 12:36 PM   #8
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by rweaver View Post
My suggestion would have been along the lines of--

Code:
for i in $(cat filename.txt); do echo $i | cut -f1-$(echo $i | awk -F\/ '{print NF-1}') -d\/; done
Which isn't an elegant solution. However, I forgot about rev and it would enable you to simplify it to a single cut as the op did. Good call!

I don't often use rev and often lament that cut doesn't support working backwards on a line... /facepalm.

Good solution OP
Sometimes you learn more from doing it yourself. I agree good job OP
 
Old 03-08-2010, 03:11 PM   #9
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
Code:
#!/bin/bash
while read url; do
    echo ${url%/*}/
done < file
 
Old 03-09-2010, 12:16 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Alternatively
Code:
basename http://blah.com/123/abc.exe 
abc.exe
Sorry; you wanted the opposite, didn't you
Code:
dirname http://blah.com/123/abc.exe
http://blah.com/123
 
Old 03-09-2010, 12:52 PM   #11
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Lots of interesting and good answers in this thread. Make sure you tag your posts up to make them easier for others to find in the future!
 
  


Reply

Tags
domain, filename, modification, path, rev, reverse, scripts, url



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
scripting question. disruptive Programming 5 02-19-2008 01:12 PM
Scripting question msandford Linux - Newbie 4 09-05-2005 10:23 AM
Scripting question buster_balz Linux - General 8 05-18-2005 03:30 PM
scripting question lazyuser Linux - General 5 01-15-2005 07:38 PM
scripting question cyph3r7 Linux - General 1 04-05-2004 02:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration