[comp.unix.questions] Working with sed

nr3m@unix.cis.pitt.edu (Matthew A Henry) (08/10/90)

Hi,

I'm writing a script that uses sed, and am having trouble with one
part.  I have a file that contains typical unix paths, one per
line, and would like to remove everything after the last forward 
slash (/).  In other words I would like the string:

	/aaa/bbb/ccc/ddd/eee

to be changed to:

	/aaa/bbb/ccc/ddd

Is this possible with sed?  Any ideas e-mailed or posted would 
be greatly appreciated.

Thanks,

Matt Henry  NR3M
(412) 443-5071
...![allegra, bellcore, cadre, idis, psuvax1]!pitt!cisunx!nr3m
NR3M@PITTUNIX
nr3m@unix.cis.pitt.edu

chris@vision.UUCP (Chris Davies) (08/10/90)

In article <27372@unix.cis.pitt.edu> nr3m@unix.cis.pitt.edu (Matthew A Henry) writes:
>I'm writing a script that uses sed,

I'm assuming by this that you don't mean 'working with a sed script'.

>and am having trouble with one
>part.  I have a file that contains typical unix paths, one per
>line, and would like to remove everything after the last forward 
>slash (/).

Why use sed?  You've said you're using a script with sed in it, so why not use
something different.  On SysV and SunOS 4.0.3_EXPORT (therefore probably BSD
systems in general??) you can use _d_i_r_n_a_m_e to do exactly what you require.

>In other words I would like the string:
>	/aaa/bbb/ccc/ddd/eee
>to be changed to:
>	/aaa/bbb/ccc/ddd

Check out _b_a_s_e_n_a_m_e too.  Thus,

	% dirname '/aaa/bbb/ccc/ddd/eee'
	/aaa/bbb/ccc/ddd/eee
	% basename '/aaa/bbb/ccc/ddd/eee'
	eee
	%

Chris
-- 
VISIONWARE LTD         | UK: chris@vision.uucp     JANET: chris%vision.uucp@ukc
57 Cardigan Lane       | US: chris@vware.mn.org    OTHER: chris@vision.co.uk
LEEDS LS4 2LE          | BANGNET:  ...{backbone}!ukc!vision!chris
England                | VOICE:   +44 532 788858   FAX:   +44 532 304676
-------------- "VisionWare:   The home of DOS/UNIX/X integration" --------------

dwn@swbatl.sbc.com (David Neill-OKCy Mktg 405-278-4007) (08/11/90)

>In article <27372@unix.cis.pitt.edu> nr3m@unix.cis.pitt.edu (Matthew A Henry) writes:
>I'm writing a script that uses sed,
>and am having trouble with one
>part.  I have a file that contains typical unix paths, one per
>line, and would like to remove everything after the last forward 
>slash (/).

sed -e 's/\/[^\/]*$//' filename

-- 
name & address   (this account) -> uunet!swbatl!dwn OR dwn@swbatl.sbc.com
David Neill       office -> 405-291-1990 -> uunet!swbatl!oktext!mktco
Mgr - Mktg.(SWBTCo) home -> 405-749-1141 -> uunet!swbatl!oktext!frodo!david

guy@auspex.auspex.com (Guy Harris) (08/12/90)

 >On SysV and SunOS 4.0.3_EXPORT (therefore probably BSD systems in
 >general??)

Nope.  SunOS picked "dirname" up from S5, not from BSD.

jeff@onion.pdx.com (Jeff Beadles) (08/16/90)

nr3m@unix.cis.pitt.edu (Matthew A Henry) writes:

|>I'm writing a script that uses sed, and am having trouble with one
|>part.  I have a file that contains typical unix paths, one per
|>line, and would like to remove everything after the last forward 
|>slash (/).  In other words I would like the string:
|>	/aaa/bbb/ccc/ddd/eee
|>to be changed to:
|>	/aaa/bbb/ccc/ddd

Of course it is.  (You can do 'most anything with sed :-)

Run the file thru this:

sed 's:/[^/][^/]*$::' < file1 > file2


Have fun!
	-Jeff
-- 
Jeff Beadles	jeff@onion.pdx.com