#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2017
    Posts
    8
    Rep Power
    0

    How can I match everything after the last occurrences of slashes and dots


    I want to extract file name `somefile` from a linux directory path such as `/home/user/directory/somefile.ext`
    without extension if it has. I manage to extract file name if it has an extension but I fail if it has no extension.
    How can I improve my oneliner to work in case of no dot at the end?

    Code:
    echo "/home/test/test/filename.exe" | grep -Po '([^/]+)(?=\.)|(?!\.+)$'
  2. #2
  3. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,884
    Rep Power
    9646
    grep is for searching through input. If you want to change input then you use sed or awk.
    Code:
    | sed 's/\.[^.]\+$//'
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2017
    Posts
    8
    Rep Power
    0
    Originally Posted by requinix
    grep is for searching through input. If you want to change input then you use sed or awk.
    Code:
    | sed 's/\.[^.]\+$//'
    Thank you for responding. That sed line prints the whole path by cutting extension. What I want is file name between final slash and final dot such as "filename". File name
    may also have dots prior to final ".exe" such as "file.name.exe" or it may not have any dot at all such as "filename"
  6. #4
  7. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,884
    Rep Power
    9646
    Ah, sorry, I forgot to mention basename.

    Thing is, writing a regex to match everything besides the extension is a bit awkward. At least when you consider a filename might have multiple extension, like filename.foo.bar.exe. Going through basename handles all that. What it can't do, unfortunately, is strip an arbitrary extension.
    Code:
    echo "/home/test/test/filename.exe" | xargs -l basename | sed 's/\.[^.]\+$//'
    I thought of another method: basename could be told what extension to strip.
    Code:
    echo "/home/test/test/filename.exex" | awk 'match($0, /(\.[^.]+)$/, m) { print $0; print m[1] }' | xargs -l2 basename
    Last edited by requinix; September 14th, 2018 at 12:41 AM.

IMN logo majestic logo threadwatch logo seochat tools logo