Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

how to shorten paths in files

  • 03-06-2011 7: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,336 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,336 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: 35,726 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