Link to home
Start Free TrialLog in
Avatar of madmare
madmareFlag for Israel

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
Avatar of nayernaguib
nayernaguib
Flag of Egypt image

Write a for loop to compare the dot '.' with every character in the string, and each time a dot is found, its location is stored in a variable. This way, after the loop ends you will have this variable contain the location of the last occurence of the dot inside the string.
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
Avatar of Julian Hansen
#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
Avatar of madmare

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
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
Avatar of buflexx
buflexx

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
set old_p="aaa/bbb/ccc/xyz.TT";
set new_p=${old_p:s/TT/KK/}
echo $new_p
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial