[comp.unix.shell] sed help

lubkt@vax1.cc.lehigh.edu (12/02/90)

A question on sed:
	I want to replace a string xyz:KJSDKJSDJS: by xyz:: in a
password file. The string "KJSDKJSDJS" is arbitrary. So I try:
	sed -e 's/xyz:\??:/xyz::' $passwd
I guess the regular expression \?? should match any string, but does
not work. I know I am goofing somewhere. Any hints? Thanks.

/Binod.
--------------------
Binod Taterway          bt00@pl118a.cc.lehigh.edu | bt00@lehigh.bitnet
User Consultant, Lehigh University, Bethlehem, PA 18015 (215) 758-3984

pfalstad@phoenix.Princeton.EDU (Paul John Falstad) (12/05/90)

In article <99.2757f296@vax1.cc.lehigh.edu> lubkt@vax1.cc.lehigh.edu writes:
>A question on sed:
>	I want to replace a string xyz:KJSDKJSDJS: by xyz:: in a
>password file. The string "KJSDKJSDJS" is arbitrary. So I try:
>	sed -e 's/xyz:\??:/xyz::' $passwd


Try:

sed '/^xyz:/s/:[^:]*:/::/' $passwd

--
From somewhere in front, you hear a repulsive squelching mumble, as if a
chattering orangutan has suddenly had its mouth crammed full of earthworms.
"You're invading my personal space!" shrieks the travel agent, rearing back
from you like an offended snake.  She is probably from California.

jeff@onion.pdx.com (Jeff Beadles) (12/06/90)

In <99.2757f296@vax1.cc.lehigh.edu> lubkt@vax1.cc.lehigh.edu writes:

>A question on sed:
>	I want to replace a string xyz:KJSDKJSDJS: by xyz:: in a
>password file. The string "KJSDKJSDJS" is arbitrary. So I try:
>	sed -e 's/xyz:\??:/xyz::' $passwd
>I guess the regular expression \?? should match any string, but does
>not work. I know I am goofing somewhere. Any hints? Thanks.

This should do what you want:


	sed 's/xyz:[^::][^::]*:/xyz::/' /etc/passwd

Some versions of sed might not need two ':''s in the "[^::]".  
The version that I'm running here complains about non-match strings that are
only one character long, sigh...

The '?' character is not special with sed.  The '.' is to sed, what the '?' is
to the shell.  Your example would change 'xyz:??abc' to 'xyz:abc'.

Hope this helps,

	-Jeff
-- 
Jeff Beadles		jeff@onion.pdx.com

tif@doorstop.austin.ibm.com (Paul Chamberlain) (12/06/90)

In article <99.2757f296@vax1.cc.lehigh.edu> lubkt@vax1.cc.lehigh.edu writes:
>	I want to replace a string xyz:KJSDKJSDJS: by xyz:: in a
>password file. The string "KJSDKJSDJS" is arbitrary. So I try:
>	sed -e 's/xyz:\??:/xyz::' $passwd

I have no idea what "\??" is supposed to do but this should work:
	sed -e 's/xyz:[^:]*:/xyz::/' $passwd

But this probably isn't what you want since this also will change
a line with "somethingxyz:KJSDKJSDJS:".  You might even want
a more general and more mischevious command like:
	sed -e 's/^\([^:]*\):[^:]*:/\1::/' $passwd

This wil remove the passwords on ALL users.  The expression "[^:]*"
matches the longest string that does not contain a colon.  A common
mistake would be to use an expression that matches more than that.

Paul Chamberlain | I do NOT represent IBM.         IBM VNET: sc30661 at ausvm6
512/838-9662     | This is rumored to work now --> tif@doorstop.austin.ibm.com