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

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

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
0
madmare
Asked:
madmare
1 Solution
 
nayernaguibCommented:
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
0
 
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
0
 
madmareAuthor Commented:
my question was in c shell
0
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 "^*\.$";
$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
0
 
nayernaguibCommented:
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
0
 
buflexxCommented:
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
0
 
ozoCommented:
set old_p="aaa/bbb/ccc/xyz.TT";
set new_p=${old_p:s/TT/KK/}
echo $new_p
0
 
ozoCommented:
echo $old_p:h.KK
0

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