Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

how to shorten paths in files

  • 03-06-2011 08:59AM
    #1
    Registered Users, Registered Users 2 Posts: 85 ✭✭


    Hi everyone,

    I am new to shell scripting and I am trying around forever now with sed and all sorts of stuff to do something that sounds really simple.

    I have a directory with a couple of files. The files contain a full url path and I need to shorten that.

    Example:

    "https://my.full.path/myFile"

    needs to be shortened to

    "myFile"

    The files contain more than just urls in them. So, I need to search for the pattern "https://my.full.path/myFile", then replace it in the same file with "myFile". There are a couple of hundred files in that directory.

    Sounds simple enough, but I cannot seem to get it together.
    Sorry for the newbie question!


Comments

  • Moderators, Technology & Internet Moderators Posts: 1,337 Mod ✭✭✭✭croo


    It's while since I was writing any serious scripts, but when I was this type of problem was resolved using Pattern-Matching Operators... that was with the korn shell but I think bash is compliant with it.

    Just tested and yes it works too in bash.

    To get myFile from "https://my.full.path/myFile"
    You would do something like
    #!/bin/bash
    path="http://my.full.path/myFile"
    echo ${path##http://*/}
    

    Google I am sure can find you the details of the operators. I think there were just 4 variants.


  • Registered Users, Registered Users 2 Posts: 3,721 ✭✭✭E39MSport


    in ksh I'd probably use

    echo "https://my.full.path/myFile" | sed 's/^.*\/\(.*\)$/\1/'

    ok unless you have millions of records. perl would possibly be handier for the latter


  • Moderators, Technology & Internet Moderators Posts: 1,337 Mod ✭✭✭✭croo


    ok I did a quick google and did not see any links to what I meant on the first screen so here is very short & quick overview.

    There were 4 "Pattern-Matching Operators" that I remember

    ${variable#pattern} - If the pattern matches the start of the variable's value, delete the shortest part that matches and return the rest.
    e.g.
    path=/home/croo/test/long.file.name
    ${path#/*/} --> croo/test/long.file.name  
    

    ${variable##pattern} - If the pattern matches the start of the variable's value, delete the longest part that matches and return the rest.
    e.g.
    path=/home/croo/test/long.file.name
    ${path##/*/} --> long.file.name
    

    ${variable%pattern} - If the pattern matches the end of the variable's value, delete the shortest part that matches and return the rest.
    e.g.
    path=/home/croo/test/long.file.name
    ${path%.*} --> /home/croo/test/long.file
    

    ${variable%%pattern} - If the pattern matches the end of the variable's value, delete the longest part that matches and return the rest.
    e.g.
    path=/home/croo/test/long.file.name
    ${path%%.*} --> /home/croo/test/long
    

    And that's it.. they are short & simple but very powerful!


  • Moderators, Arts Moderators Posts: 36,205 Mod ✭✭✭✭pickarooney


    Can't you just use the basename command or am I missing something?


  • Registered Users, Registered Users 2 Posts: 297 ✭✭stesh


    Try the cut command:
    % echo "https://my.full.path/myFile" | cut -d '/' -f 4
    myFile
    

    Here -d specifies the delimiter, and -f specifies which portion of the string you want to retain. You can also select multiple portions:
     echo "https://my.full.path/myFile" | cut -d '/' -f 2,4
    /myFile
    
    (portions 2 and 4)
    % echo "https://my.full.path/myFile" | cut -d '/' -f 2-4
    /my.full.path/myFile
    
    (the portions between 2 and 4, inclusive)


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,721 ✭✭✭E39MSport


    But then OP is constrained to a fixed path length/format?


  • Registered Users, Registered Users 2 Posts: 201 ✭✭jonathan11


    Nice feature in ksh for doing this...

    ##### Strip everything before last slash:
    path="https://my.full.path/myFile"
    echo ${path##/*/}

    (as croo said above)


Advertisement