# substitute first character of filename with another character



## cgjoker (Aug 13, 2003)

hi there.

im trying to move a file with the name of 0wf_file.dat to bwf_file.dat

The 0 value may be 1 to 9 so it will change all the time but the end file name will always be lead by the letter 'b'.

so it may be 

0wf_file.dat > bwf_file.dat
1wf_now.dat > bwf_now.dat
2wf_later.dat > bwf_later.dat

I thought this might do it but im not having any luck.

for f in $0*.* 
do 
cd /test
sed -n 's/0/b/' $f
done

for f in $1*.* 
do 
cd /test
sed -n 's/0/b/' $f
done

so on....


----------



## Squashman (Apr 4, 2003)

Look at the example I gave you in this thread.
http://forums.techguy.org/showthread.php?t=335819


----------



## cgjoker (Aug 13, 2003)

Shoot, I forgot about that one. Is there a way I can look at all my previous tickets so I don't have to bug you guys?

By the way, here is what I came up with but it only works if I have one file. I may have many files though. Ill keep working on it.

cd /test

for FILE in `ls | grep 0*.*`;
do
newname=`echo $FILE | tr -d "0"`
mv $FILE b$newname
done


----------



## Squashman (Apr 4, 2003)

cgjoker said:


> By the way, here is what I came up with but it only works if I have one file. I may have many files though. Ill keep working on it.


Click on your user name and then select Find all posts.


----------



## Squashman (Apr 4, 2003)

cgjoker said:


> cd /test
> 
> for FILE in `ls | grep 0*.*`;
> do
> ...


Read the man page. You can substitute that Zero for a an inclusive number.
"[:digit:]"

You dont even have to use the -d either. The Translate command by defaults translates the first set to the second set if you read the man page.

This should work but your way will work as well.
tr "[:digit:]" "b"

Take the grep out of there as well.


----------



## cgjoker (Aug 13, 2003)

Thanks again. I actually figured out both how to do my script and how I can find out what my previous posts are.

Here is what I came up with for this problem. I used the sed command instead.

cd /test

for filename in * 
do
echo "$filename" | grep -q " " 
if [ $? ] 
then
fname=$filename .
n=`echo $fname | sed -e "s/^0/b/g"` 
mv "$fname" "$n" 
fi
done

thanks again for your help.


----------



## Squashman (Apr 4, 2003)

Yeah, I figured SED could do it too but I try to keep things as simple as possible. Mine is slightly shorter but yours actually works better because it will only match the Zero at the beginning of the line. You could put in a range of digits in your sed statement to match any digit at the beginning of the line. I am trying to remember how to do that I think it would be
sed -e "s/^[0-9]/b/g"


----------



## cgjoker (Aug 13, 2003)

Good idea.. ill try that out the range thing as well to see if it works. That would be useful.

Thanks again.


----------



## Squashman (Apr 4, 2003)

I cannot get your script to run. Not sure why you are doing it the way you have it either. Just seems like a lot of extra wasted code.


----------



## cgjoker (Aug 13, 2003)

It worked by the way. Thanks.


----------



## cgjoker (Aug 13, 2003)

its a little different now..

cd /test

for filename in * 
do
if [ $filename ] 
then 
n=`echo $filename | sed -e "s/^[0-9]/b/g"` 
mv "$filename" "$n" 
fi
done


----------



## cgjoker (Aug 13, 2003)

Got another problem on the same issue.

Im trying to move a file from one directory to another, while checking the name and date condition, and replacing the first character to a 'b' in the process.

Problem is the echo is returning the filename as ./file rather than file, so the sed isn't finding a number in the beginning of the filename, but rather the ./

Here is the code im trying to execute.

for file in `find . -type f -mtime +1 -name "1*.*"` 
do 
if [ $file ]
then 
n=`echo $file | sed -e "s/^[0-9]/b/g"` 
echo $n
mv "$file" "$n" 
mv $n ./dir 
fi
done


----------



## tsunam (Sep 14, 2003)

you can always do a

n=`echo $file | sed -e "s/^\.//" | sed -e "s/^[0-9]/b/g"`

that should escape on the period and replace everything before it with nothing. *sighs at laptop heading towards Memphis, tenn 

So i'm not entirely sure that it'll work but should get you closer to doing what you want again.


----------



## Squashman (Apr 4, 2003)

Or you could pipe it to the cut or tr commands to get rid of the *./*. But sed should work fine as well.

Weren't you working on another script with moving files from the previous day. I remember you said you could not get it to run. Did you ever get that to run. I was wondering if it was because I am using bash and you are using korn shell.

http://forums.techguy.org/showthread.php?t=339068

You wouldn't even need to use SED to do this. I am a SED lover but I also subscribe to the K.I.S.S religion.


```
for file in `find . -type f -mtime +1 -name "1*.*"` 
do 
if [ $file ]
then 
n=`echo $file | cut -b3-` 
echo $n
mv "$file" "b$n" 
mv b$n ./dir 
fi
done
```


----------



## tsunam (Sep 14, 2003)

go go learning something new...cut is a new friend in the repertoire.


----------



## cgjoker (Aug 13, 2003)

well, ill try the cut code out. unfortunately though the sed command isn't working.

when I echo the file name, here is what i get:

/1file1.dat
/1file2.dat

the / is still present, so the mv command isn't going to work.

i was thinking of putting the two sed commands together, but im gonna try the cut code first.

as for the moving files issue, yes, i got it to work using this code you see here. this issue here came up after that one.


----------



## cgjoker (Aug 13, 2003)

i tried the cut code you gave Squashman, but its not working for me. unfortunately its only removing the period and not the slash. ill keep working on it.


----------



## cgjoker (Aug 13, 2003)

so the cut works now... i just increased the bytes position by 1 so cut -b4- instead of cut -b3-

thanks to both of you for your help.


----------

