Howdy, Stranger!

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

Categories

Extracting filename

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

filename.java

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

How do I do that in Bash.

Thanks.

Comments

  • abcabc Member Posts: 443
    [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.
  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

    _______ [ http://forcoder.org ] free ebooks and video tutorials about ( Go Scratch MATLAB PHP Perl PL/SQL C# C Python R Visual Basic C++ Delphi Ruby Java Assembly Swift JavaScript Visual Basic .NET Objective-C ML Erlang Apex FoxPro LabVIEW Lisp ABAP Scheme Bash SAS Clojure Prolog Lua Awk Transact-SQL Scala Crystal D Rust COBOL Kotlin Ada Logo VBScript Julia Dart Alice Fortran F# Hack ) ______

Sign In or Register to comment.