![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
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
Code:
# ./test.sh xxx_yyy_aaa_bbb_04302007.txt |
|
||||
|
Quote:
Thanks for your help.Could you please explain the symbols #,% you had used in the script. Thanks again, Kumar |
|
||||
|
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.
Nagarajan Ganesan. |
|
||||
|
Quote:
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|