Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2004
    Posts
    1

    Unanswered: remove the file extension from file name

    I am getting the file name in a variable
    i need to remove the extension and append a string to it.
    file= file1.xml

    i need to trim it and append "_output.txt" to it,without changing the file file1.

    Output should be file1_output.xml

    I jus need to modify the file name string.

    Please write me as of how to do this

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Code:
    #!/bin/ksh
    
    newExt='_output.txt'
    
    file=file1.xml
    
    echo "before->[$file]"
    file=${file%%.*}${newExt}
    echo "after->[$file]"
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  3. #3
    Join Date
    Feb 2004
    Posts
    52
    Hi.

    Vlad, I had the same problem and your post solved the puzzle.
    could you just explain what "%%" does in your command ?

    file=${file%%.*}${newExt}

    Thanks,

    S.

  4. #4
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    'man ksh' and search for 'Parameter Expansion'

    Code:
         ${parameter%%word}
                   Remove Largest Suffix Pattern. The  word  will  be
                   expanded  to  produce  a  pattern.  The  parameter
                   expansion then will result in parameter, with  the
                   largest  portion of the suffix matched by the pat-
                   tern deleted.
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  5. #5
    Join Date
    Nov 2004
    Posts
    2
    I'm wondering if I can use this for my problem? It's a little different, but I can't seem to figure out how to do it. I'm trying to copy a changing list of files from one directory to another while adding a variable prefix and a date suffix to a file name before the file extension. The problem is the files might have multiple extensions.

    For example, I have two files:
    test1.zip
    test2.txt.pgp

    I would like to move them to the other directory while changing the name. So I would like the copied files to be:

    MAP_test1_20041110.zip
    MAP_test2_20041110.txt.pgp

    I can change the file name fine. But can't figure out how to maintain the file extensions.

    Here's what I have so far:
    fname=test2.txt.pgp
    comp_sym="MAP"

    newfilename=$comp_sym"_"${fname%%.*}"_"`date +%Y%m%d`
    (this will get me MAP_test2_20041110 but can't figure out how to get those varied file extensions)

    Thanks

  6. #6
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    newfilename="${comp_sym}_${fname%%.*}_$(date +%Y%m%d).${fname#*.}"
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  7. #7
    Join Date
    Nov 2004
    Posts
    2
    Wow, that was easy. Thanks! Your a lifesaver.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •