If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > remove the file extension from file name

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-03-04, 09:41
aminroop aminroop is offline
Registered User
 
Join Date: Nov 2004
Posts: 1
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
Reply With Quote
  #2 (permalink)  
Old 11-03-04, 10:05
vgersh99 vgersh99 is offline
Registered User
 
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> |
+-----------------------+
Reply With Quote
  #3 (permalink)  
Old 11-04-04, 09:36
Serg Serg is offline
Registered User
 
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.
Reply With Quote
  #4 (permalink)  
Old 11-04-04, 09:44
vgersh99 vgersh99 is offline
Registered User
 
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> |
+-----------------------+
Reply With Quote
  #5 (permalink)  
Old 11-11-04, 03:10
joesc1 joesc1 is offline
Registered User
 
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
Reply With Quote
  #6 (permalink)  
Old 11-11-04, 08:48
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
newfilename="${comp_sym}_${fname%%.*}_$(date +%Y%m%d).${fname#*.}"
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #7 (permalink)  
Old 11-12-04, 01:49
joesc1 joesc1 is offline
Registered User
 
Join Date: Nov 2004
Posts: 2
Wow, that was easy. Thanks! Your a lifesaver.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On