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
renaming xls file systemsb UNIX for Dummies Questions & Answers 2 10-19-2007 08:50 AM
Renaming a file with spaces patwa UNIX for Dummies Questions & Answers 2 06-21-2007 07:50 PM
Help in renaming file !!! kumarsaravana_s UNIX for Dummies Questions & Answers 5 05-02-2007 01:45 PM
Renaming a file name dbrundrett Shell Programming and Scripting 2 01-06-2004 10:36 AM
Renaming a file to the same name lachino8 UNIX for Dummies Questions & Answers 3 07-31-2002 09:52 AM

Closed Thread
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 09-30-2008
anushree.a anushree.a is offline
Registered User
  
 

Join Date: May 2008
Posts: 87
renaming file

Dear Friends,
Need your help once again.

I have this file name

e.g.1) report_12.rp_1
e.g.2) remark_mm.rmr_3


I want it to be renamed as

report_12_1.rp
remark_mm_3.rmr
  #2 (permalink)  
Old 09-30-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
If you have the file name in $filename you can do the following.


Code:
head1=${filename%_[0-9]}
tail=${filename#$head1}
head2=${head1%.*}
ext=${head1#$head2}
mv "$filename" "${head2%$ext}$tail$ext"

This assumes the final suffix is a single digit. The construct ${var#pat} means the value of $var with any match on the pattern pat removed from the beginning; ${var%pat} does the corresponding substitution on the end of the value. See the manual page for your shell for more information.
  #3 (permalink)  
Old 09-30-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,337
With sed:


Code:
sed 's/\(.*\)\(\..*\)\(_.*\)/\1\3\2/'

For example:


Code:
echo report_12.rp_1 | sed 's/\(.*\)\(\..*\)\(_.*\)/mv & \1\3\2/' | sh

Regards

Last edited by Franklin52; 09-30-2008 at 05:03 AM.. Reason: adding example
  #4 (permalink)  
Old 09-30-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,556
if you have Python , you can download this script.
example usage for same file name structure:

Code:
# ls -1 re*
remark_mm.rmr_19
remark_mm.rmr_3
report_12.rp_1

# filerenamer.py -p "(.*_.*)(\..*)(_.*)$" -e "\\1\\3\\2" -l "re*_*"  #remove -l to commit
==>>>>  [ /home/remark_mm.rmr_3 ]==>[ /home/remark_mm_3.rmr ]
==>>>>  [ /home/remark_mm.rmr_19 ]==>[ /home/remark_mm_19.rmr ]
==>>>>  [ /home/report_12.rp_1 ]==>[ /home/report_12_1.rp ]

# ls -1 re*
remark_mm_19.rmr
remark_mm_3.rmr
report_12_1.rp

Closed Thread

Bookmarks

Tags
python, rename files

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 09:16 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