?
Solved

unix spliting string variable

Posted on 2005-03-22
8
Medium Priority
?
284 Views
Last Modified: 2008-03-04

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
Comment
Question by:madmare
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 14

Expert Comment

by:nayernaguib
ID: 13603715
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
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 13603846
#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
 

Author Comment

by:madmare
ID: 13603871
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!

 
LVL 10

Expert Comment

by:Andrew Beers
ID: 13604154
$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
 
LVL 14

Expert Comment

by:nayernaguib
ID: 13605728
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
 
LVL 1

Expert Comment

by:buflexx
ID: 13613096
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
 
LVL 84

Expert Comment

by:ozo
ID: 13613210
set old_p="aaa/bbb/ccc/xyz.TT";
set new_p=${old_p:s/TT/KK/}
echo $new_p
0
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 13613239
echo $old_p:h.KK
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Starting up a Project
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question