Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace file name within unix script

Status
Not open for further replies.

confuseddddd

Programmer
May 22, 2003
53
US
Have file where month is part of file name, example:AAA06BBB.TXT and want to be able to change the month (in this case from 06 to 07), each month within the script, not changing filename each month manually.
Currently, some one physically changes the file
name and I want to have the script change the file name.

Any suggestions?????
 
Code:
# rename a filename
new=`echo $old | awk '{
  a=substr($1,0,3)
  b=substr($1,4,2)
  c=substr($1,6);
  printf("%s%02d%s\n",a,(b+1)%12,c)}'`
mv $old $new

The
Code:
(b+1)%12
part wraps December round to January, but the precise form depends on whether months are numbered 00 to 11, or 01 to 12.
 
Tried to execute the awk command but I am receiving errors...
rename[2]: old: not found.

And new is printing 01

Here is the script....can you see what I did wrong???

Thanks.


# rename a filename
old = AAA05BBB.TXT
print $old
new=`echo $old | awk '{
a=substr($1,0,3)
b=substr($1,4,2)
c=substr($1,6);
printf("%s%02d%s\n",a,(b+1)%12,c)}'`

print $new
#mv $old $new
 
Watch the spaces and quotes
Code:
$ old='AAA05BBB.TXT'
$ echo $old
AAA05BBB.TXT
 
Tried it the way indicated and still receiving errors:

# rename a filename
$ old = 'AAA05BBB.TXT'
$ echo $old
new=`echo $old | awk '{
a=substr($1,0,3)
b=substr($1,4,2)
c=substr($1,6);
printf("%s%02d%s\n",a,(b+1)%12,c)}'`

echo $new
#mv $old $new


RESULTS:
rename[2]: $: not found.
rename[3]: $: not found.
01


Then tried another way:
old='AAA05BBB.TXT'
echo $old
new=`echo $old | awk '{
a=substr($1,0,3)
b=substr($1,4,2)
c=substr($1,6);
printf("%s%02d%s\n",a,(b+1)%12,c)}'`

echo $new
#mv $old $new


And received the results I expected:
AAA05BBB.TXT
AAA06BBB.TXT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top