The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
mv command to rename multiple files that retain some portion of the original file nam grimace15 Shell Programming and Scripting 4 12-21-2008 06:27 AM
Rename file based on first 3 characters of data in file jchappel UNIX for Dummies Questions & Answers 3 10-01-2008 02:21 PM
rename file to file.ext.datetime tripsat Shell Programming and Scripting 2 09-20-2007 04:12 PM
Not able to rename file MANISH KU Shell Programming and Scripting 2 06-27-2007 04:07 AM
Help with multiple file rename - change case of part of file name steve7 UNIX for Dummies Questions & Answers 7 06-30-2005 01:41 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-09-2009
yshahiac yshahiac is offline
Registered User
  
 

Join Date: Jun 2008
Location: US
Posts: 6
Rename multiple file from file listing

I am new at Linux/UNIX programming. Here is my problem.
I had one big file which I split using the command

csplit -k -s -f april.split. april '/^ISA/' '{10000}'

So now I have multiple files with names

april.split.01
april.split.02
april.split.03

But I need the name of the file like

CHRY.850.01
CHRY.850.02
CHRY.850.03

etc.
Can I use following commands on the prompt or do I have to write a script.

for each in `cat april-850-split`
do
cp $each ${each}.850


Thanks
  #2 (permalink)  
Old 02-09-2009
quirkasaurus's Avatar
quirkasaurus quirkasaurus is offline
Registered User
  
 

Join Date: Jan 2009
Location: canton, michigan
Posts: 373
Yes, you can run this type of stuff from the command line.

You'll get a secondary prompt and it's smart enough to know
you're done when it sees the "done" command.

Preferred, however, is this:

Code:
cat april-850-split |
while read each ; do

cp $each $each.850

done
... because then you'll never get the "arglist too long" error.

BTW -- don't think this renaming will give you exactly what you wanted....

don't you need:

Code:
number=${each##*.}
mv $each CHRY.850.$number
But again, I prefer doing things like this:

Code:
cat april-850-split |
while read each ; do

  number=${each##*.}
  echo mv $each CHRY.850.$number

done |
  tee commands.sh
This'll save my intended "mv" ( rename ) commands in a script file named commands.sh

Then I can visually inspect it --- make sure it's going to do what I want... then run it,
saving all the output to an "err" file:

Code:
ksh -xvf commands.sh 2>&1 | tee err
  #3 (permalink)  
Old 02-09-2009
yshahiac yshahiac is offline
Registered User
  
 

Join Date: Jun 2008
Location: US
Posts: 6
Thanks for your reply. You are correct. I do need CHRY.850.01 etc. When I ran you code, I am getting

mv april.split.03 CHRY.850.
mv april.split.04 CHRY.850.
mv april.split.05 CHRY.850.

without the number.

Here is my code.

$ cat april-850-list | while read each; do number=${each##*.} echo mv $each CHRY.850.$number done | tee repl.sh;

Can you please tell me what is wrong with my code? I ma running on Linux. I don't think that would make a difference.


Thanks
  #4 (permalink)  
Old 02-09-2009
quirkasaurus's Avatar
quirkasaurus quirkasaurus is offline
Registered User
  
 

Join Date: Jan 2009
Location: canton, michigan
Posts: 373
You need to show the contents of the april-850-list file.

What SHELL on Linux?
I'm on Solaris...

We may have to try something like:

number=` echo $each | awk -F. '{print $NF }'`
  #5 (permalink)  
Old 02-09-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by yshahiac View Post
I have multiple files with names

april.split.01
april.split.02
april.split.03

But I need the name of the file like

CHRY.850.01
CHRY.850.02
CHRY.850.03

Can I use following commands on the prompt or do I have to write a script.

There is no difference between a script entered on the command line and a script in a file. (Apart from the occasional quoting issue.)

Code:
for file in april.split.*
do
  mv "$file" "CHRY.850.${file##*.}"
done
  #6 (permalink)  
Old 02-09-2009
yshahiac yshahiac is offline
Registered User
  
 

Join Date: Jun 2008
Location: US
Posts: 6
Thank you. It worked.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:09 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0