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 > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

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 07:50 AM
renaming part of a file ajaya Shell Programming and Scripting 0 04-11-2006 10:00 PM
renaming the file with the timestamp ajubi Shell Programming and Scripting 6 12-05-2005 06:21 AM
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 08:52 AM

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 05-02-2007
kumarsaravana_s kumarsaravana_s is offline
Registered User
  
 

Join Date: Feb 2007
Location: Bangalore
Posts: 105
Help in renaming file !!!

Hi All,

I want to rename a file inside a script which has a date portion appended at the start of the file name.

The script i wrote works fine when the file comes on a day to day basis but sometimes it comes late too.

#!/usr/bin/ksh

cd /space/file/source

dt=$(date "date "+%m%d%Y")
mv $dt_xxx_yyy_aaa_bbb.txt xxx_yyy_aaa_bbb_$dt.txt

input:
04302007_xxx_yyy_aaa_bbb.txt

output should be like
xxx_yyy_aaa_bbb_04302007.txt.

I know sed or awk can do this by extracting the numeric part and then appending it to the end of file name.But me totally new to such things.Can someone help me extracting the date portion and then adding it just before the extension?

Thanks,
Kumar
  #2 (permalink)  
Old 05-02-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,507
just one of the many ways:
Code:
newfile=$( echo 04302007_xxx_yyy_aaa_bbb.txt  |awk 'BEGIN { FS = "."}
						 {	
							n = split($1,s1,"_")
							for(i=2;i<=n;i++) printf "%s_",s1[i]
							printf "%s.%s\n",s1[1] , $2
						 }' )
echo $newfile
output:
Code:
# ./test.sh
xxx_yyy_aaa_bbb_04302007.txt
  #3 (permalink)  
Old 05-02-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
$ nm=04302007_xxx_yyy_aaa_bbb.txt
$ flnm=${nm#*_}
$ dt=${nm%%_*}
$ ext=${nm##*.}
$ echo ${flnm%.*}"_"${dt%?}"."${ext}
xxx_yyy_aaa_bbb_0430200.txt
  #4 (permalink)  
Old 05-02-2007
kumarsaravana_s kumarsaravana_s is offline
Registered User
  
 

Join Date: Feb 2007
Location: Bangalore
Posts: 105
Quote:
Originally Posted by anbu23
Code:
$ nm=04302007_xxx_yyy_aaa_bbb.txt
$ flnm=${nm#*_}
$ dt=${nm%%_*}
$ ext=${nm##*.}
$ echo ${flnm%.*}"_"${dt%?}"."${ext}
xxx_yyy_aaa_bbb_0430200.txt
Hi Anbu,

Thanks for your help.Could you please explain the symbols #,% you had used in the script.

Thanks again,
Kumar
  #5 (permalink)  
Old 05-02-2007
ennstate ennstate is offline
Registered User
  
 

Join Date: Mar 2007
Location: Chennai
Posts: 222
From the man pages,

Code:
       ${name#pattern}, ${name##pattern}
              If pattern matches the beginning of the value of parameter name,
              the matched text is deleted from the result of substitution.   A
              single  #  results in the shortest match, two #’s results in the
              longest match.

       ${name%pattern}, ${name%%pattern}
              Like ${..#..} substitution, but it deletes from the end  of  the
              value.
Thanks
Nagarajan Ganesan.
  #6 (permalink)  
Old 05-02-2007
kumarsaravana_s kumarsaravana_s is offline
Registered User
  
 

Join Date: Feb 2007
Location: Bangalore
Posts: 105
Quote:
Originally Posted by ennstate
From the man pages,

Code:
       ${name#pattern}, ${name##pattern}
              If pattern matches the beginning of the value of parameter name,
              the matched text is deleted from the result of substitution.   A
              single  #  results in the shortest match, two #’s results in the
              longest match.

       ${name%pattern}, ${name%%pattern}
              Like ${..#..} substitution, but it deletes from the end  of  the
              value.
Thanks
Nagarajan Ganesan.
Thanks Nagaraj.
Sponsored Links
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 02: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