madmare
asked on
unix spliting string variable
Hi,
How do i make a new variable contain the same path and file name but diffrent type.
for example:
the old path is: aaa/bbb/ccc/xyz.TT
the new one will be: aaa/bbb/ccc/xyz.KK
my old files end with .TT
the new end with .KK
#include <string.h>
int main ()
{
char oldpath[] = " aaa/bbb/ccc/xyz.TT" ;
char newpath[] = "aaa/bbb/ccc/xyz.KK" ;
int i ;
strcpy ( newpath, oldpath ) ;
for ( i = strlen (newpath) - 1; i > 0 && newpath[i] != '.'; i-- ) ;
strcpy ( &newpath[i], ".KK" ) ;
return 0 ;
}
Should do the trick
int main ()
{
char oldpath[] = " aaa/bbb/ccc/xyz.TT" ;
char newpath[] = "aaa/bbb/ccc/xyz.KK" ;
int i ;
strcpy ( newpath, oldpath ) ;
for ( i = strlen (newpath) - 1; i > 0 && newpath[i] != '.'; i-- ) ;
strcpy ( &newpath[i], ".KK" ) ;
return 0 ;
}
Should do the trick
ASKER
my question was in c shell
$oldPath = "aaa/bbb/ccc/xyz.TT";
$newPath = echo $oldPath | grep "^*\.$";
$newExtension="KK"
$newPath = $newPath $newExtension
echo $newPath
#Will print:
#aaa/bbb/ccc/xyz.KK
#the grep command pulls aaa/bbb/ccc/xyz. then the $newPath $newExtension concatenates them.
Hope this helps out!
~Aqua
$newPath = echo $oldPath | grep "^*\.$";
$newExtension="KK"
$newPath = $newPath $newExtension
echo $newPath
#Will print:
#aaa/bbb/ccc/xyz.KK
#the grep command pulls aaa/bbb/ccc/xyz. then the $newPath $newExtension concatenates them.
Hope this helps out!
~Aqua
Here's a C Shell script that will read all file names in the current directory with the old extension (TT) and replace it with the new one (KK) and print the modified file names:
__________________________ __________ __________
#!/bin/csh
foreach fullname(*.TT)
set newname=`basename "$fullname" .TT`
echo $newname.KK #or do whatever you like with the newname variable
end
exit 0
__________________________ __________ __________
If you want the script to rename the files, just replace the echo statement with the following one (although it is *much* easier to rename files with the mv command!):
mv $fullname $newname.KK
Also, to store the new file name in the newname variable, replace the echo statement with the following:
set newname=$newname.KK
________________
Nayer Naguib
__________________________
#!/bin/csh
foreach fullname(*.TT)
set newname=`basename "$fullname" .TT`
echo $newname.KK #or do whatever you like with the newname variable
end
exit 0
__________________________
If you want the script to rename the files, just replace the echo statement with the following one (although it is *much* easier to rename files with the mv command!):
mv $fullname $newname.KK
Also, to store the new file name in the newname variable, replace the echo statement with the following:
set newname=$newname.KK
________________
Nayer Naguib
madmare,
question is very simle, just one command line in csh :
set new_p=`expr $old_p : '\(.*\.\)TT$'`KK;
full c0de:
#!/bin/csh
set old_p="aaa/bbb/ccc/xyz.TT" ;
set new_p=`expr $old_p : '\(.*\.\)TT$'`KK;
echo "$new_p";
//buflexx
question is very simle, just one command line in csh :
set new_p=`expr $old_p : '\(.*\.\)TT$'`KK;
full c0de:
#!/bin/csh
set old_p="aaa/bbb/ccc/xyz.TT"
set new_p=`expr $old_p : '\(.*\.\)TT$'`KK;
echo "$new_p";
//buflexx
set old_p="aaa/bbb/ccc/xyz.TT" ;
set new_p=${old_p:s/TT/KK/}
echo $new_p
set new_p=${old_p:s/TT/KK/}
echo $new_p
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Now replace the characters following the dot with any other characters, and don't forget to terminate the string with a '\0' character (null).
If you want the new file name to be stored in a new variable, all you need to do is copy the string variable into another string variable before replacing any characters.
_________________
Nayer Naguib