Re: sed Issue .. (delimiter) (362 Views)
Reply
Regular Advisor
rleon
Posts: 125
Registered: ‎07-27-2006
Message 1 of 7 (397 Views)
Accepted Solution

Sed Issue ..

I everyone I need some help with a sed statement.

 

This is a scirpt that updates the /etc/shadow on linux and solaris.

 

And I am having probems with one line.

 

ssh -q ${HOST} 2>/dev/null "rm /tmp/shadow.*"
ssh -q ${HOST} 2>/dev/null "cp /etc/shadow /tmp/shadow.bak"

 

ssh -q ${HOST} 2>/dev/null "sed -e '/^root:/s/:[^:]*:/:${PASS}:/' /tmp/shadow.bak > /tmp/shadow.new"

Most of the time it works without any issues but for some reason everyone once in a while /tmp/shadow.new is empty. The file is there .. just nothing in it.

 

-rw-------   1 root     root           0 Sep 14 10:32 /tmp/shadow.new

 

Is there something wrong with my sed staement?

Or am I missing something?


Thanks

Ricardo

 

 

 

 

    

Regular Advisor
rleon
Posts: 125
Registered: ‎07-27-2006
Message 2 of 7 (389 Views)

Re: Sed Issue ..

Im a little closer ..

 

it seems that the issue happens when the password hash has a \ or / in it.

 

I get the following errors:

 

sed: command garbled:

 

or

 

sed: -e expression #1, char 29: Unknown option to `s'

 

Acclaimed Contributor
Dennis Handly
Posts: 25,074
Registered: ‎03-06-2006
Message 3 of 7 (386 Views)

Re: sed Issue .. (delimiter)

[ Edited ]

>it seems that the issue happens when the password hash has a \ or / in it

 

You could replace that sed "/" by a comma.

Regular Advisor
rleon
Posts: 125
Registered: ‎07-27-2006
Message 4 of 7 (381 Views)

Re: Sed Issue ..

Hi Dennis

 

Can you clarify ..

 

If the password hash is

abcdef123/ABC

 

And I change it to

abcdef123,ABC

 

That makes the password different and doesnt work.

Acclaimed Contributor
Dennis Handly
Posts: 25,074
Registered: ‎03-06-2006
Message 5 of 7 (372 Views)

Re: sed Issue .. (delimiter)

[ Edited ]

>Can you clarify?

 

Change your sed command to use "," as the delimiter:

ssh -q ${HOST} 2> /dev/null "sed -e '/^root:/s,:[^:]*:,:${PASS}:,' /tmp/shadow.bak > /tmp/shadow.new"

Regular Advisor
rleon
Posts: 125
Registered: ‎07-27-2006
Message 6 of 7 (369 Views)

Re: sed Issue .. (delimiter)

Thanks !!!

 

That worked like a charm !!

 

Woud you mind explaining what the comma does?

 

Acclaimed Contributor
Dennis Handly
Posts: 25,074
Registered: ‎03-06-2006
Message 7 of 7 (362 Views)

Re: sed Issue .. (delimiter)

>Would you mind explaining what the comma does?

 

The same as the "/".  :-)

You can use any char as the delimiter, as long as it matches.  From sed(1):

      (2)s/regular expression/replacement/flags
                  Substitute replacement string for instances of regular
                  expression in the pattern space.  Any character can be
                  used instead of /.  For a fuller description see ed(1).

 

It seems you can even use a ";", though it is normally a command separator.

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.