Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split Filename and extension in KSH 2

Status
Not open for further replies.

davism

MIS
Nov 9, 2002
140
US
It is possible to split the filename and extension in KSH? If so, how?

I have a script called test.ksh

in the script I have:

echo $0

The $0 returns "test.ksh"

But how can I get just the item, as a variable, to the left of the "." while retaining the contents of the item to the right of the "." in a variable.

In Perl, it can be done with:

($filename, $file_ext) = split/\./,$original_file;

But how could/would I do this in KSH?

Any assistance would greatly be appreciated.
 
Worked out GREAT! But can you do me a favor and explain that cut statement?

Would be greatly appreciated.

Thanks
 
Glad to help. The cut takes the first 'field' (-f1) of the text and prints everything before the first defined delimiter (-d'.')

Have a good weekend!
 
Great! Thanks for the explanation and very helpful!
 
Thutmose,

Appreciate the additional info.

Can you provide an explanation on this one as well?

Would be greatly appreciated.

Thanks
 
echo $0 | awk -F . '{print $1}'

awk parses the line test.ksh, while the "-F ." tells awk to use "." as a field seperator, which then effectively splits the "test.ksh" into "test" and "ksh" for awk, which reads "test" as "$1" and "ksh" as "$2".

'{print $}' tells awk to print the value of "$1" which is "test"

 
Hi,

And another one (only works with ksh):

filename=${original_file%%.*}
(search the pattern matching ".*" at the end of the variable's value, delete the longuest part of it and return the rest)

file_ext=${original_file##*.}
(the same from the beginning)

If someone could be nice enough to give the solution with sed ... ;-)
 
A sed solution shouldn't be hard.
Code:
filename=`echo $0 | sed 's/^\(.*\)\.\([^.]*\)/\1/'`
fileext=`echo $0 | sed 's/^\(.*\)\.\([^.]*\)/\2/'`
//Daniel
 
KenCunningham and Thutmose,

how would I assign the results of your solutions to a variable?

chk_val = $0 | cut -f1 -d'.'

or

chk_val = $0 | awk -F. '{print $1}'

when I try that I can't get anything that I can echo to confirm (i.e. echo chk_val)

Any information on that?
 
KenCunningham and Thutmose,

how would I assign the results of your solutions to a variable?

chk_val = $0 | cut -f1 -d'.'

or

chk_val = $0 | awk -F. '{print $1}'

when I try that I can't get anything that I can echo to confirm (i.e. echo chk_val)

Any information on that?
 
Daniel,

Can you elaborate a little more? Please.

Thanks
 
I tried that as below:

chk_val = $(echo $0 | awk -F. '{print $1}')

It's giving me a:

chk_val: {kshname} {line #} not found

Where {kshname} and {line #} are dependant upon what I am using for the name and the line # of where things are at.

What am I doing wrong?
 
The problem is/was a space before and after the "=". I didn't realize that having those would cause an issue.

Thanks for the assistance and the information you provided.

Very helpful.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top