The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Stripping out extensions when file has multiple dots in name Nemelis Shell Programming and Scripting 8 05-14-2008 05:12 AM
Truncate multiple file extensions prvnrk Shell Programming and Scripting 12 04-04-2008 07:20 AM
batch file ramneek IP Networking 1 11-08-2005 07:09 AM
File name extensions thurrock Shell Programming and Scripting 2 11-07-2001 09:05 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-29-2008
Registered User
 

Join Date: Apr 2008
Posts: 7
Stumble this Post!
Unhappy 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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-29-2008
robotronic's Avatar
Can I play with madness?
 

Join Date: Apr 2002
Location: Italy
Posts: 370
Stumble this Post!
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.
Reply With Quote
  #3 (permalink)  
Old 04-29-2008
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 549
Stumble this Post!
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
Reply With Quote
  #4 (permalink)  
Old 04-29-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,602
Stumble this Post!
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.
Reply With Quote
  #5 (permalink)  
Old 04-29-2008
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 549
Stumble this Post!
era: you got me, my bad
Reply With Quote
  #6 (permalink)  
Old 05-19-2008
Registered User
 

Join Date: Apr 2008
Posts: 7
Stumble this Post!
Talking

great, thanks a lot guys !
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0