Results 1 to 7 of 7

Thread: Shell Script Help

  1. #1
    Join Date
    Mar 2008
    Location
    Back to India
    Beans
    278
    Distro
    Ubuntu Gnome 14.04 Trusty Tahr

    Shell Script Help

    Hi,

    I want to download some files using the wget command. I want to create a shell script, which will extract all the required parameters from the given URL of download location.
    eg.
    http://WEBSITE/FIRSTDIR/FILENAME(ABCD.ABC).zip

    Out of this I want to extract
    wget http://WEBSITE/FIRSTDIR/FILENAME(ABCD.ABC).zip -O FILENAME -a Filename

    1. Here the source file name contains the (ABCD.ABC) which I don't want into the -O or -a output filename.
    2. The FIRSTDIR can be /FIRSTDIR/SECONTDIR/..../..... ans so on. Out of that the last / contains the file name.

    Thanks in advance .

  2. #2
    Join Date
    Oct 2009
    Location
    here and there...
    Beans
    67
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Shell Script Help

    Code:
    #!/bin/bash
    
    text="http://WEBSITE/FIRSTDIR/FILENAME(ABCD.ABC).zip"
    
    ext=`echo "$text" | awk -F"." '{print $NF}'`
    remove="(`echo $text | awk -F"(" '{print $NF}' | sed -e 's/.'$ext'//g'`"
    filename_ext=`echo $text | awk -F"/" '{print $NF}' | sed -e 's/'$remove'//g'`
    filename_only=`echo $text | awk -F"/" '{print $NF}' | sed -e 's/'$remove'//g' | sed -e 's/.'$ext'//g'`
    
    echo -e "ext\t\t= $ext\nremove\t\t= $remove\nfilename_ext\t= $filename_ext\nfilename_only\t= $filename_only"
    and this is the output of running it:

    Code:
    ext        = zip
    remove        = (ABCD.ABC)
    filename_ext    = FILENAME.zip
    filename_only    = FILENAME
    it doesn't matter how big is the folder tree it will still get this data as it get's what's after the last slash "/" to work with
    i suppose you can figure it out from here
    this was so you can understand something... so next time you can do it yourself.. now here's how to get it in one line:



    Code:
    filename=`echo $text | awk -F"/" '{print $NF}' | sed -e 's/('$(echo $text | awk -F"(" '{print $NF}' | sed -e 's/.'$(echo "$text" | awk -F"." '{print $NF}')'//g')'//g'`
    and if you echo the $filename variable you'll get:
    Code:
    FILENAME.zip
    Last edited by blchinezu; June 2nd, 2010 at 01:02 PM.

  3. #3
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Shell Script Help

    slightly simpler sed expression to do it in one line:
    Code:
    $ in="http://WEBSITE/FIRSTDIR/FILENAME(ABCD.ABC).zip"
    $ out=$(echo $in | sed -r 's/^(.*\/)([^(]*)(.*)$/wget \0 -O \2 -a \2/')
    $ echo $out
    wget http://WEBSITE/FIRSTDIR/FILENAME(ABCD.ABC).zip -O FILENAME -a FILENAME

  4. #4
    Join Date
    Oct 2009
    Location
    here and there...
    Beans
    67
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Shell Script Help

    Quote Originally Posted by DaithiF View Post
    slightly simpler sed expression to do it in one line:
    Code:
    $ in="http://WEBSITE/FIRSTDIR/FILENAME(ABCD.ABC).zip"
    $ out=$(echo $in | sed -r 's/^(.*\/)([^(]*)(.*)$/wget \0 -O \2 -a \2/')
    $ echo $out
    wget http://WEBSITE/FIRSTDIR/FILENAME(ABCD.ABC).zip -O FILENAME -a FILENAME
    and that's kind of a sed master i don't know that much but i manage to do the thing.. still your script is better

  5. #5
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Shell Script Help

    And with parameter expansions:
    Code:
    #!/bin/bash
    url='http://WEBSITE/FIRSTDIR/FILENAME(ABCD.ABC).zip'
    filename=${url##*/} filename=${filename%%(*} ext=${url##*.}
    wget -O "$filename.$ext" -a "$filename.log" "$url"
    http://mywiki.wooledge.org/BashFAQ/073

  6. #6
    Join Date
    Mar 2008
    Location
    Back to India
    Beans
    278
    Distro
    Ubuntu Gnome 14.04 Trusty Tahr

    Re: Shell Script Help

    Thanks to all of you.

    I was knowing that there is some way to solve this equation, was not getting the solution.

    Thanks to all of you again.

    Regards,

    Vishal Agarwal


  7. #7
    Join Date
    Oct 2009
    Location
    here and there...
    Beans
    67
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Shell Script Help

    Quote Originally Posted by geirha View Post
    thanks for that link... it's really useful

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •