Operating System - HP-UX
1833775 Members
3715 Online
110063 Solutions
New Discussion

Re: remove the last field from a string

 
SOLVED
Go to solution
Kris_5
Occasional Advisor

remove the last field from a string

Hi Folks,

I have a string like the following
$BACD_APPL/xyz/abc/abc.txt

Now I need to print the string on the screen without the last field (abc.txt)
i.e. the output should be $BACD_APPL/xyz/abc/

I was able to do it using cut and sed, but it comes as two commands. Is any body has single line command solution.

TIA
4 REPLIES 4
Rodney Hills
Honored Contributor
Solution

Re: remove the last field from a string

dirname abc/def/ghi

will display
abc/def

Hope this helps...

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: remove the last field from a string

Another way within ksh is

x=/xyz/abc/abc.txt
echo ${x%/*}

will display
/xyz/abc

-- Rod Hills
There be dragons...
harry d brown jr
Honored Contributor

Re: remove the last field from a string


And don't forget you can use

basename

to grab the last portion off:

abc.txt

live free or die
harry

Live Free or Die
H.Merijn Brand (procura
Honored Contributor

Re: remove the last field from a string

And use the modifiers in (t)csh:

/W 56 > foreach i ( /W/sv.txt )
foreach? echo "r" $i:r
foreach? echo "e" $i:e
foreach? echo "t" $i:t
foreach? echo "h" $i:h
foreach? end
r /W/sv
e txt
t sv.txt
h /W
/W 57 >
Enjoy, Have FUN! H.Merijn