fnsplit, Extract filename from path

Hi all,

I know this has been covered a lot, I have been searching and reading for hours on the subject, however so far I have been unsuccessful at accomplishing the goal using sed.

I know this can be done with parameter expansion (Thanks cfajohnson for a great explanation of parameter expansion).

# getfn
str='./filename.ext'
fn=${str##*/}

However the goal is sed'ucation!

I am here...

# getfn
str='./filename.ext'
fn=$(echo ${str} | sed 's,/.*/,,')

Then I combine that with

# stripext
str='./filename.ext'
fn=$(echo ${str} | sed 's,\(.*\)\..*,\1,')

Like

# getpurefn
 str='./filename.ext'
fn=$(echo ${str} | sed 's,/.*/,,;s,\(.*\)\..*,\1,')

For the ultimate goal of the pure 'filename'...

All works good except when the path begins with './'..

What would be the resolution??

I'm not getting a good understanding of RE's yet so an explanation of how the expression works would be awesome as well! (This is my first attempt at RE's)

Thanks all.

-Enjoy
fh : )_~

Hi.

sed "s,.*/\(.*\),\1,"

Or without the extention:

sed "s,.*/\(.*\)\..*,\1,"

If you want the "pure" filename, what's wrong with basename?

basename ./test
 
test

Or did I miss something?

str='./filename.file.ext'
# echo $str | sed "s,.*/\(.*\)\..*$,\1,"
filename.file

scottn, Thank you, Obliviously I don't need to say "It works"!, About basename, yup, you did miss something... the sed'ucation part!
This really is just for education with sed and RE's.

danmero, Thank you, Again, you know I don't need to say "It works"!

To both!
I thank you, not only for this answer but the others I have gleaned from this site that may very well have been yours!

Anyways, A couple things stand out to me...

  1. scottn's method has no '$' ... If I remember correctly the '$' means EOL.
  2. Both use double quotes (") versus single (')

Super clean method ... I was way off! :frowning:

Does anyone know of a workbook or something that has sed and RE projects to work through??

-Enjoy
fh : )_~

Hi!

The $ isn't neccessary (regular expressions are greedy and "dot rest of line" (\..) is the same as "dot rest of line to the end of the line"! (\..$)) because it will match the longest expression it can from the first dot it finds (hence the greedy bit).

Single, double, doesn't matter unless there's something you want the shell to expand somewhere - in which case you'd use double quotes.

Google.... there's lots of stuff out there. (or look around on this site. my RE's are anything but great, but if there's an answer to be had, you could probably find it here)

Understood ... Makes sense, Thank you!

Yup, I understood this, wanted to ensure there was no other reason...
I typically use double quotes unless I know it needs single anyways.
If I'm fighting something and there is no parameters to expand I'll use singles.

I don't Google, too much garbage! ... I use altavista... :slight_smile:

Thats the reason I almost always include something along the lines of:
"Thank you all for the answers to my unasked questions!"

And I very much mean that to everyone!

Finding it can get tough if you don't know exactly what you are looking for!

I have found a slight issue with the RE...

sh scriptname.sh

LOGFILE=$(echo ${0} | sed 's,.*/\(.*\)\..*$,\1,').log
echo $LOGFILE

scriptname.sh.log

oooPs, Found out the hard way... :frowning:
Had to manually rename things! :eek:

Thanks

-Enjoy
fh : )_~