LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 11-11-2010, 09:17 AM   #1
tuxtutorials
Member
 
Registered: Dec 2008
Location: New York
Distribution: RedHat, Solaris
Posts: 68

Rep: Reputation: 16
Better way to do this?


Hello all,

Need some input from how others would solve the following issue:

I needed to download a file using wget, however the url was something like:

Code:
https://content.web.webserve.com/web/public/NULL/package-name/anotherdir/x86_64/tomcat5-webapps-5.5.23-0jpp.14.el5.x86_64.rpm?__gda__=128942323423423423406_8d5a0asdfasdfasdf678cef05e0&ext=.rpm
and the file was being saved as such. Did some reading and figured out you can use the -O to save output to custom format. However I did not want to manually input each file name so I came up with the following non-intelligent shell script:

Code:
#!/bin/bash

URL_IN=$1
OUTPUTFILE_NAME=`echo "$URL_IN" | awk -F? '{ print $1 }' | awk -F/ '{ print $NF }'`

wget -O $OUTPUTFILE_NAME $URL_IN

exit
It works but in my opinion the awk code is non-efficient and could have probably be done in one pipe or even more efficiently. My goal here was to strip off tomcat5-webapps-5.5.23-0jpp.14.el5.x86_64.rpm. Can someone chime in and give some pointers on a better way to do this?

Thanks
 
Old 11-11-2010, 09:34 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Code:
echo $URL_IN | awk 'gsub(/.*\/|?.*/,"")'
 
Old 11-11-2010, 09:38 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
It would run faster using bash parameter expansion (because no need for creating new processes):
Code:
c@CW8:~$ buf='https://content.web.webserve.com/web/public/NULL/package-name/anotherdir/x86_64/tomcat5-webapps-5.5.23-0jpp.14.el5.x86_64.rpm?__gda__=128942323423423423406_8d5a0asdfasdfasdf678cef05e0&ext=.rpm'
c@CW8:~$ buf=${buf%%\?*} && echo $buf
https://content.web.webserve.com/web/public/NULL/package-name/anotherdir/x86_64/tomcat5-webapps-5.5.23-0jpp.14.el5.x86_64.rpm
c@CW8:/etc$ buf=${buf##*/} && echo $buf
tomcat5-webapps-5.5.23-0jpp.14.el5.x86_64.rpm
 
Old 11-15-2010, 02:20 PM   #4
tuxtutorials
Member
 
Registered: Dec 2008
Location: New York
Distribution: RedHat, Solaris
Posts: 68

Original Poster
Rep: Reputation: 16
Thank you very much colucix, catkin for the input, both approaches work perfectly!
 
  


Reply

Tags
awk, bash, script, wget


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



All times are GMT -5. The time now is 02:46 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration