Extracting filename

I want to extract the name of the file before the extension. That is


I want to extract only the name 'filename' and discard the extension '.java'.

How do I do that in Bash.



  • [b][red]This message was edited by abc at 2002-10-16 18:58:16[/red][/b][hr]
    echo "filename.java" | sed -n 's/(.*).[^.]*$/1/p'

    the replace command cuts the last dot and all that comes after it

    [blue]correction[/blue] - the previous version does not display files that have no extension (like "filename"). To do that use:

    echo "filename.java" | sed -n 's/(.*).[^.]*$|$/1/p'

    translated, sed searches ('s/') for the pattern '(.*).[^.]*$|$' - meaning 'any letter' (.) 'zero or more times' (*) followed by either a dot ('.'), then any non-dot letter ('[^,]) zero or more times and end of line ('$') or directly the end of line; then replaces this pattern with 1, which is the text matching the pattern between the first ( and ) pair.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion