Announcement

Collapse
No announcement yet.

Using sed to strip sftp from url

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • jlittle
    replied
    Not exactly what you asked, Xplorer4x4, but why use subprocesses and pipes when bash can do it natively and cleanly:
    Code:
    strippedurl=/${url#sftp://*/}
    One sees that sed stuff a lot in Gnu scripts which have to run on the old Bourne shell, and can't use bashisms. People hack configure scripts and pick up practices like that from 20 years ago. However, the ${x#removed} construct is POSIX IIUC.

    Leave a comment:


  • james147
    replied
    Originally posted by kubicle View Post
    Another option would be something like:
    Code:
    stripedurl=`echo "${url}" | sed "s/^sftp:\/\/[^\/]*//"`
    That would be better, though I tend to replace the seperator character with one that doesn't apear so often in the pattern like:
    Code:
    stripedurl=`echo "${url}" | sed "s=^sftp://[^/]*=="`
    as it makes reading it easier.

    To be honest I am not sure why I didn't jump to this solution right away now should teach me to not pay that much attention to what I write.
    Last edited by james147; Oct 20, 2012, 03:51 PM.

    Leave a comment:


  • kubicle
    replied
    Another option would be something like:
    Code:
    stripedurl=`echo "${url}" | sed "s/^sftp:\/\/[^\/]*//"`
    or (for easier readability):
    Code:
    stripedurl=`echo "${url}" | sed "s+^sftp://[^/]*++"
    or:
    Code:
    stripedurl=`echo "${url}" | sed "s+^.*//[^/]*++"`
    (I'm sure a ninja could make it shorter still )
    Last edited by kubicle; Oct 20, 2012, 03:54 PM.

    Leave a comment:


  • james147
    replied
    Originally posted by kubicle View Post
    It would probably work with 'sed -r'
    Ahh, yes, I was swearing that the regex was correct but couldn't figure out why it didn't work... I should really read the man pages more often.

    Code:
    stripedurl=`echo "${url}" | sed -r 's+sftp://(\w*@)?\w*(\.\w*)?++'`
    Is what you want to use Xplorer4x4, that should reduce all of the following:
    sftp://user@host.domain/a/path/on/the/server
    sftp://user@host/a/path/on/the/server
    sftp://host.domain/a/path/on/the/server
    sftp://host/a/path/on/the/server
    to
    /a/path/on/the/server
    Last edited by james147; Oct 20, 2012, 03:46 PM.

    Leave a comment:


  • kubicle
    replied
    Originally posted by james147 View Post
    It is ment to strip sftp://user@hostname.domain/ from the start of a path not just the sftp:// part.
    Right you are, of course

    It would probably work with 'sed -r'
    Last edited by kubicle; Oct 20, 2012, 02:40 PM.

    Leave a comment:


  • james147
    replied
    It is ment to strip sftp://user@hostname.domain/ from the start of a path not just the sftp:// part.

    Leave a comment:


  • kubicle
    replied
    Originally posted by Xplorer4x4 View Post
    I am trying to use a service menu that calsl a bash script and logs in to a remote server and executes a command on the file or folder. It should work but sed
    Code:
        stripedurl=`echo "${url}" | sed 's+sftp://\w*@\w*(.\w*)?++'`
    Is that supposed to do something like this?:
    Code:
    stripedurl=`echo "${url}" | sed "s/^sftp:\/\///g"`

    Leave a comment:


  • Xplorer4x4
    started a topic Using sed to strip sftp from url

    Using sed to strip sftp from url

    I am trying to use a service menu that calsl a bash script and logs in to a remote server and executes a command on the file or folder. It should work but sed is not stripping sftp from the url path.
    Code:
    url=${1}
        echo "url: $url" # debug line
    
        if echo "$url" | grep -Ev "s+sftp://\w*@\w*/+/+"; then
    echo "Not a valid remote path: $url"
            return 1
        fi # This part checks for the sftp://user@host and returns if it is not found, this should stop it from running on files not accessed via sftp
    
        stripedurl=`echo "${url}" | sed 's+sftp://\w*@\w*(.\w*)?++'`

Users Viewing This Topic

Collapse

There are 0 users viewing this topic.

Working...
X