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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell Script to rename files yakuzaa Shell Programming and Scripting 2 03-21-2009 01:33 PM
Shell script to rename files with .1,.2,.3 ....ext respectively satyajit007 Shell Programming and Scripting 4 02-06-2009 06:43 AM
rename multiple filename.45267.txt to >> filename.txt jason7 Shell Programming and Scripting 6 01-29-2009 05:37 PM
How to Rename/Convert Files in Shell Scripting? hanu_oracle Shell Programming and Scripting 11 10-21-2008 10:13 AM
rename files using shell scripting gfhgfnhhn Shell Programming and Scripting 4 07-04-2006 05:37 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-16-2009
in2vtec in2vtec is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 2
Post how to rename all files that have a certain text in the filename using tcsh shell

Hello~

I'm on AIX version 5 and I believe I have the tcsh shell environment to play in. Can you guys help me with a solution to rename all files that have "eclp" in the filename to "ecl" ? I basically want to rename the files and strip the "p" out.

i.e. original filenames:

grp_eclp_grp_cent_ADTA.xlt grp_seri_grp_eclp_ADTB.xlt
grp_eclp_grp_cern_ADTA.xlt grp_seri_grp_eclp_ADTC.xlt
grp_eclp_grp_cern_ADTB.xlt grp_sftmd_grp_eclp_ADT.xlt


altered filenames when done:

grp_ecl_grp_cent_ADTA.xlt grp_seri_grp_ecl_ADTB.xlt
grp_ecl_grp_cern_ADTA.xlt grp_seri_grp_ecl_ADTC.xlt
grp_ecl_grp_cern_ADTB.xlt grp_sftmd_grp_ecl_ADT.xlt


Thanks in advance!
  #2 (permalink)  
Old 07-16-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
Why you ask to use tcsh, if you have nothing ? Even your interactive command shell is tcsh, you can do your script using any shell which you have. And execute it even you use tcsh.

Example, I'm sure that you have sh in /bin directory, so this works, even it is ksh88 version or BourneShell - save this text to file rename.sh :

Code:
#!/bin/sh
#rename.sh
for f in $*
do
      newname=`echo "$f" | sed "s/^grp_eclp_/grp_ecl_/" `
      if [ "$newname" = "$f" ] ; then
           echo "no rename $f"
           continue
      fi
      echo "rename $f -> $newname"
      mv  "$f"  "$newname"
done

And after you have created file rename.sh , give execute priviledges and then call rename.sh with those files which you like to rename.

Code:
chmod a+rx rename.sh
./rename.sh  grp*ADTA*.xlt
# or
./rename.sh grp*.xlt
# or ...


Last edited by kshji; 07-16-2009 at 02:56 PM..
  #3 (permalink)  
Old 07-17-2009
in2vtec in2vtec is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 2
Kshji,

Thanks for your help.

One of team members used the following (can you tell me what the difference is between your method and his?):

#!/usr/bin/sh

# usage: call ./rename.sh .*.xlt
for file in *.xlt
do
newfile=`echo $file | sed s/$1/$2/g`
echo is $newfile
mv $file $newfile
done
  #4 (permalink)  
Old 07-17-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
Both are almost perfect .
My version use dynamic filenames, your teams dynamic replace rule.
Your team version not chech, is there name change or not. My version check it.

So this is the best:

Code:
#!/bin/sh
#rename.sh
# arg1=old string
# arg2=new string
# arg3-n = files to make name editing
old="$1"
new="$2"
# remove 2 args, rest are filenames
shift 2
for f in $*
do
      newname=`echo "$f" | sed "s/$old/$new/" `
      if [ "$newname" = "$f" ] ; then
           echo "no rename $f"
           continue
      fi
      echo "rename $f -> $newname"
      mv  "$f"  "$newname"
done

Using:

Code:
./rename.sh "grp_eclp_" "grp_ecl_"  grp*ADTA*.xlt

Reply

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 07:32 AM.


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