LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-08-2008, 03:10 PM   #1
VTGuitarMan
LQ Newbie
 
Registered: Mar 2008
Distribution: Debian, Ubuntu
Posts: 4

Rep: Reputation: 0
Shell script for modifying file extensions


Hi All,

I've been using Linux/Unix operating systems for several years now, just as a general-purpose OS and for my research in computational biochemistry. That said, I have little coding experience and have only recently begun trying to write simple shell scripts to automate some processes.

I have run into a bit of a hang-up in a script I'm trying to write. I would like to modify the extensions of a (large) series of files. I have read a number of threads about changing extensions (JPG --> jpg is a common one), but my problem is a bit different.

I have a series of files:

file.pdb.1
file.pdb.2
....
file.pdb.x

A .pdb file is a simple text file with atomic coordinates, so essentially it is just a text file. I would like to take these files (automatically output by another program, and I have no control over the output nomenclature) to names like:

file_1.pdb
file_2.pdb
(etc)

Does anyone know a simple way to do this (or even a complicated one)? I'm at a loss! Since the number after .pdb keeps changing, I haven't been able to come up with a simple search string to do an iterative find and replace.

Thanks in advance.
 
Old 03-08-2008, 03:17 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi,

Welcome to LQ!

Code:
for i in $(ls file*pdb*);
do
  new=$(echo $i| sed -r 's/([^.]+)\.([^.]+)\.([^.]+)/\1_\3.\2/' )
  mv $i $new
done

Cheers,
Tink
 
Old 03-08-2008, 03:53 PM   #3
VTGuitarMan
LQ Newbie
 
Registered: Mar 2008
Distribution: Debian, Ubuntu
Posts: 4

Original Poster
Rep: Reputation: 0
Wow, thanks for the quick reply, Tink! It's exactly what I needed and things are working great!
 
Old 03-08-2008, 05:48 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
You're welcome, and glad it did :}

Was untested, and wouldn't have worked if there
were e.g. spaces in the file-names ;}


Cheers,
Tink
 
Old 03-08-2008, 06:13 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
I will precede the "mv" command with echo just to test it out. If you make a typo you might rename all of the files to the same thing, loosing all but one file.
Code:
for file in demo.pdb.*; do
   echo mv -v "$file" "${file%.pdb.*}_${file##*.}.pdb"
done
For this version, the files are named demo.pdb.1, demo.pdb.2, etc. It uses variable expansion instead of an external program. Note the double quotes around the filenames. If you have variable whose value contains white space, you need to surround the variable with double quotes to prevent shell taking the value as multiple arguments.
After running the test you can hit the up arrow to bring back the command and delete the echo command.

Tinkster:
what is with the ls command?
Code:
for i in $(ls file*pdb*);
I'm sorry but that is one of my pet peeves.

Last edited by jschiwal; 03-08-2008 at 06:16 PM.
 
Old 03-08-2008, 07:53 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Heh. Shell-globbing would suffice indeed. Old habit ;}


Cheers,
Tink
 
Old 03-08-2008, 08:24 PM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
My habit is calling everything "file" because I've used something similar to the above so many times.

Then I'll start with "for dir" but my finger memory will type "file" instead latter on instead of "dir". I think using "echo" was the more important point when working interactively. Imagine accidentally adding a space to an argument and having it expand to "rm ~/$olddocs /*". Or having a series of move commands expanded as: "mv file1.JPG file.JPG" "mv file2.JPG file.JPG". Especially if you are using a complicated sed command to convert the filename.

Yes, this is a case where I have learned the hard way.

Last edited by jschiwal; 03-08-2008 at 08:25 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[Shell] Read a File from script yussef Programming 4 08-19-2008 05:26 AM
Need a shell script to turn a lined file into a comma seperated file Thaidog Linux - General 8 03-08-2008 08:19 PM
File Size Shell Script BlackLabel Programming 7 11-27-2007 08:48 PM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 11:37 PM
modifying a file in shell (via sed ?) Shautieh Programming 7 09-19-2006 06:14 AM


All times are GMT -5. The time now is 03:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration