Batch changing file extensions | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Batch changing file extensions

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-29-2008
cdines cdines is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 2 July 2008, 8:23 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Data Batch changing file extensions

We are moving from an OpenVMS server to a Unix server and I have a problem with ftp'ing files.

When I ftp the VMS server from the Unix server, I need to "mget" some files, for example "mget test_file*.txt;". The semicolon is necessary because OpenVMS has multiple versions of the file (eg test_file.txt;1, test_file.txt;2, etc).

When I do this i end up with the file "test_file.txt;2". I then need to convert this to remove the semicolon.

I have tried various methods and have found one that works :

for file in *.txt*; do
noext="${file%.*}"
mv "$file" "${noext#*.}.txt"
done

However, I have problems with this :

1. it renames ALL .txt files even if they have no ";" and version number.
2. it will only do one file extension at a time.
3. it is inefficient

What I want is to say "for all files with a semicolon in the file extension, rename the file to everything to the left of the semicolon".

Better yet, "give me all files matching *.txt; from theVMS server, and create them on the Unix server without the ;"

Any suggestions would be appreciated.
Sponsored Links
    #2  
Old 04-29-2008
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
 
Join Date: Apr 2002
Last Activity: 20 December 2009, 10:56 AM EST
Location: Italy
Posts: 370
Thanks: 0
Thanked 0 Times in 0 Posts
Try this:


Code:
ls *.*\;* | awk -F';' '{ printf("mv \"%s\" \"%s\"\n", $0, $1) }' > rename.sh

Check if the output is what you want and then:


Code:
sh rename.sh

However, the code must be changed a bit if you have more than one semicolon in file names.
Sponsored Links
    #3  
Old 04-29-2008
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 21 March 2014, 6:30 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 117 Times in 110 Posts
Quote:
Originally Posted by cdines View Post
What I want is to say "for all files with a semicolon in the file extension, rename the file to everything to the left of the semicolon".
The foolowing code should do the job.

Code:
for file in `ls ./*\;*`
do
   mv "$file" "${file%;*}"
done

    #4  
Old 04-29-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
danmero: *bling*, Useless Use of ls in Backticks.


Code:
for file in *\;*
do
  mv "$file" "${file%;*}"
done

cdines: The best-case scenario, as you note, would be to find a ftp client which understands the VMS file naming convention and can convert it to Unix, but I have never heard of one which would do that. Ask in VMS circles, though; they would know.
Sponsored Links
    #5  
Old 04-29-2008
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 21 March 2014, 6:30 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 117 Times in 110 Posts
era: you got me, my bad
Sponsored Links
    #6  
Old 05-19-2008
cdines cdines is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 2 July 2008, 8:23 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Computer

great, thanks a lot guys !
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
rename file, add extensions jaituteja Shell Programming and Scripting 4 02-05-2012 06:41 PM
File extensions in a dir ganga.dharan Shell Programming and Scripting 5 11-06-2009 11:31 AM
changing extensions for multiple files at once Straitsfan UNIX for Dummies Questions & Answers 1 04-22-2009 03:39 PM
Batch Renaming: Change files' extensions in many sub-directories dave920 UNIX for Dummies Questions & Answers 10 07-21-2008 12:00 AM
File name extensions thurrock Shell Programming and Scripting 2 11-07-2001 12:05 PM



All times are GMT -4. The time now is 10:30 AM.