Splitting a srting at the last occurance of "/"

  • Follow


All.

I need to seperate the path from the file name - extension in a shell
script.

So far I have ${0%.*} to give me the file name and path, and
${0#*.} me the extension.

Depending on how I call the script (test.sh) the first of the above
either

../test

or 

/home/scripts/test

what I now need to do is split it into two

../ or /home/scripts

and 

test

Just catching the last four characters is not the answer as I am trying
to make this generic, e.g The ccript name could be "slilyscript.scr" in
such a case I wold just want the "sillyscript" bit seperated from it's
path.


May be AWK is not the way I don't know, any help or pointers
appreciated.

Robert
-- 

0
Reply Rob 12/3/2005 11:10:00 AM

Rob Bradford wrote:
> All.
> 
> I need to seperate the path from the file name - extension in a shell
> script.

Wrong newsgroup, this is a shell question, use comp.unix.shell instead.

> So far I have ${0%.*} to give me the file name and path, and
> ${0#*.} me the extension.

Both are wrong if you want a split at the path delimiter / and not at
the extension . (dot). Use

   ${0##*/}  for the directory path and
   ${0%/*}   for the program name


Janis


> Depending on how I call the script (test.sh) the first of the above
> either
> 
> ./test
> 
> or 
> 
> /home/scripts/test
> 
> what I now need to do is split it into two
> 
> ./ or /home/scripts
> 
> and 
> 
> test
> 
> Just catching the last four characters is not the answer as I am trying
> to make this generic, e.g The ccript name could be "slilyscript.scr" in
> such a case I wold just want the "sillyscript" bit seperated from it's
> path.
> 
> 
> May be AWK is not the way I don't know, any help or pointers
> appreciated.
> 
> Robert
0
Reply Janis 12/3/2005 12:03:42 PM


Janis Papanagnou wrote on 03/12/2005 in comp.lang.awk:

> Rob Bradford wrote:
> > All.
> > 
> > I need to seperate the path from the file name - extension in a
> > shell script.
> 
> Wrong newsgroup, this is a shell question, use comp.unix.shell
> instead.
> 
> > So far I have ${0%.*} to give me the file name and path, and
> > ${0#*.} me the extension.
> 
> Both are wrong if you want a split at the path delimiter / and not at
> the extension . (dot). Use
> 
>   ${0##*/}  for the directory path and
>   ${0%/*}   for the program name
> 
> 
> Janis
> 
> 
> > Depending on how I call the script (test.sh) the first of the above
> > either
> > 
> > ./test
> > 
> > or   /home/scripts/test
> > 
> > what I now need to do is split it into two
> > 
> > ./ or /home/scripts
> > 
> > and   test
> > 
> > Just catching the last four characters is not the answer as I am
> > trying to make this generic, e.g The ccript name could be
> > "slilyscript.scr" in such a case I wold just want the "sillyscript"
> > bit seperated from it's path.
> > 
> > 
> > May be AWK is not the way I don't know, any help or pointers
> > appreciated.
> > 
> > Robert

OK,

I did say I wasn't sure if this was an AWK thing or not. I'll repost in
a shell group.

Thanks for the pointer.

-- 

0
Reply Rob 12/3/2005 12:16:20 PM

Rob Bradford wrote:
> Janis Papanagnou wrote on 03/12/2005 in comp.lang.awk:
>>Rob Bradford wrote:
>>
>>>I need to seperate the path from the file name - extension in a
>>>shell script.
>>
>>Wrong newsgroup, this is a shell question, use comp.unix.shell
>>instead.
>>
>>>So far I have ${0%.*} to give me the file name and path, and
>>>${0#*.} me the extension.
>>
>>Both are wrong if you want a split at the path delimiter / and not at
>>the extension . (dot). Use
>>
>>  ${0##*/}  for the directory path and
>>  ${0%/*}   for the program name
> 
> I did say I wasn't sure if this was an AWK thing or not. I'll repost in
> a shell group.

I gave you the information about the shell newsgroup because you
don't seem to have been aware of its existance.

The two patterns I gave you above will already solve your task.

Janis
0
Reply Janis 12/3/2005 12:35:56 PM

Janis Papanagnou wrote on 03/12/2005 in comp.lang.awk:

> Rob Bradford wrote:
> > Janis Papanagnou wrote on 03/12/2005 in comp.lang.awk:
> > > Rob Bradford wrote:
> > > 
> > > > I need to seperate the path from the file name - extension in a
> > > > shell script.
> > > 
> > > Wrong newsgroup, this is a shell question, use comp.unix.shell
> > > instead.
> > > 
> > > > So far I have ${0%.*} to give me the file name and path, and
> > > > ${0#*.} me the extension.
> > > 
> > > Both are wrong if you want a split at the path delimiter / and
> > > not at the extension . (dot). Use
> > > 
> >> ${0##*/}  for the directory path and
> >> ${0%/*}   for the program name
> > 
> > I did say I wasn't sure if this was an AWK thing or not. I'll
> > repost in a shell group.
> 
> I gave you the information about the shell newsgroup because you
> don't seem to have been aware of its existance.
> 
> The two patterns I gave you above will already solve your task.
> 
> Janis

I knew about the group, I just felt this was an AWK issue as I wanted
the program name minus extension i.e "test" from /home/scripts/test.sh
as this involved loosing stuff at both ends of the sting I directed it
at the AWK group as I felt that was twhere it belonged.

I'm trying to genericly split the name of a script $0 into three parts

i	The path
ii	The extension
iii	The filename minus extension.

I've got i and ii but can't get iii.

Rob

-- 

0
Reply Rob 12/3/2005 1:20:40 PM

Rob Bradford wrote:
>>>
>>>>Rob Bradford wrote:
>>>>
>>>>>I need to seperate the path from the file name - extension in a
>>>>>shell script.

I read your posting as if the above was the only you want to achieve.

> I knew about the group, I just felt this was an AWK issue as I wanted
> the program name minus extension i.e "test" from /home/scripts/test.sh
> as this involved loosing stuff at both ends of the sting I directed it
> at the AWK group as I felt that was twhere it belonged.
> 
> I'm trying to genericly split the name of a script $0 into three parts
> 
> i	The path
> ii	The extension
> iii	The filename minus extension.
> 
> I've got i and ii but can't get iii.

See my reply to your posting in comp.unix.shell how to obtain iii.

Janis
0
Reply Janis 12/3/2005 1:31:02 PM

5 Replies
458 Views

(page loaded in 0.039 seconds)

Similiar Articles:













7/25/2012 7:09:08 AM


Reply: