Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

unix spliting string variable

 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
1 Solution
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
Julian HansenCommented:
#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
madmareAuthor Commented:
my question was in c shell
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Andrew BeersTechnology LeadCommented:
$oldPath = "aaa/bbb/ccc/xyz.TT";
$newPath = echo $oldPath | grep "^*\.$";
$newPath = $newPath $newExtension

echo $newPath

#Will print:
#the grep command pulls aaa/bbb/ccc/xyz. then the $newPath $newExtension concatenates them.

Hope this helps out!

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:


foreach fullname(*.TT)
        set newname=`basename "$fullname" .TT`
        echo $newname.KK #or do whatever you like with the newname variable
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

question is very simle, just one command line in csh :
set new_p=`expr $old_p : '\(.*\.\)TT$'`KK;

full c0de:
set old_p="aaa/bbb/ccc/xyz.TT";

set new_p=`expr $old_p : '\(.*\.\)TT$'`KK;

echo "$new_p";

set old_p="aaa/bbb/ccc/xyz.TT";
set new_p=${old_p:s/TT/KK/}
echo $new_p
echo $old_p:h.KK

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now