LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-18-2003, 12:02 AM   #1
uribo
LQ Newbie
 
Registered: Apr 2003
Posts: 11

Rep: Reputation: 0
Unhappy sed with delimiter, bourne


this line is part of /etc/shadow file -->
shelly:$$/gtrygDi$rtklncAD.lv3w4Nr6nG17.:12147:0:99999:7::12783:

i need to sed the "encrypted password" (i.e. the second field) and replace it with a "*".

becos of the delimiter, i have problem.

can anyone tell me if sed can do the work? if not, what function can i use? thanks!
 
Old 04-18-2003, 11:00 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,340

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Try your text editor

I have changed that field with my text editor, kwrite. Once I screw it up I usually use my rescue system command line text editor, mp, to fix it.
 
Old 04-19-2003, 01:12 AM   #3
cuckoopint
Member
 
Registered: Feb 2003
Distribution: Debian
Posts: 797

Rep: Reputation: 30
ok, you need to explain more.

Code:
awk -F : '{print $2}' /etc/shadow    #this will list all second 'row' entries
#I guess you can grep for 'shelly' (or some other matching pattern)
sed -i s/$var/* /etc/shadow
is that what you want to do? I recommend trying this out on some temporary file first...
; )
 
Old 04-19-2003, 09:26 AM   #4
uribo
LQ Newbie
 
Registered: Apr 2003
Posts: 11

Original Poster
Rep: Reputation: 0
hi,

yes, that is what i want... but can i have a one line command?

 
Old 04-19-2003, 09:33 AM   #5
cuckoopint
Member
 
Registered: Feb 2003
Distribution: Debian
Posts: 797

Rep: Reputation: 30
make a bash script, or use the ';' or '&&'

ie.

ls blah; grep blah && cp blah

(I would recommend the script)

; )
 
Old 04-19-2003, 09:44 AM   #6
uribo
LQ Newbie
 
Registered: Apr 2003
Posts: 11

Original Poster
Rep: Reputation: 0
hi,

yes, it is part of my script. i tried this:

`cat /etc/shadow |awk -F: '{print $2}' |grep "^shelly" | sed "s/"$2"/*/"` > temp.doc && mv temp.doc /etc/shadow

it erases my whole file... why???
 
Old 04-19-2003, 11:15 AM   #7
cuckoopint
Member
 
Registered: Feb 2003
Distribution: Debian
Posts: 797

Rep: Reputation: 30
ok, the problem I have is that I don't know how to quote variable in sed. It doesn't seem to like acting like everyone else.

Code:
#!/bin/sh
for i in `grep "^shelly" shadow | awk -F: '{print $2}'`
do
        echo $i
        sed -i s/"$i"/*/ /etc/shadow
done
Now, you had a couple problems. First, you must grep, then awk.
and after 'sed', you cannot move the file back. (because you would be only moving the * to /etc/shadow). Instead you use the -i option to replace within the file. Again, the only problem is that sed doesn't like "$i"

BTW, I only put the echo in there for testing.
; )
 
Old 04-19-2003, 10:37 PM   #8
uribo
LQ Newbie
 
Registered: Apr 2003
Posts: 11

Original Poster
Rep: Reputation: 0
hi,

thanks very much for yur prompt reply. i tried many ways, in the end, i have to do this:

cat /etc/shadow | sed "/^"shelly"/ s/^\([a-zA-Z]*:\)\([a-zA-Z0-9$/.]*:\)\([0-9]*:\)\([0-9]*:\)\([0-9]*:\)\([0-9]*:\)\([0-9]*:\)\([0-9]*:$\)/\1*\:\3\4\5\6\7\8/" > temp.doc && mv temp.doc /etc/shadow

if you happened to figure out how to call a variable, please post here again. thanks!


 
Old 04-19-2003, 11:30 PM   #9
uribo
LQ Newbie
 
Registered: Apr 2003
Posts: 11

Original Poster
Rep: Reputation: 0
I did it... almost giving up hope!


for i in `grep "^shelly" /etc/shadow | awk -F: '{print $2}'` ; do
sed 's_'"$i"'_*_' /etc/shadow
done
 
Old 04-19-2003, 11:51 PM   #10
cuckoopint
Member
 
Registered: Feb 2003
Distribution: Debian
Posts: 797

Rep: Reputation: 30
Cool.

But didn't you want that to be sed -i 's_ ... ?

Well, thanks for the heads up.
 
Old 04-20-2003, 12:16 AM   #11
uribo
LQ Newbie
 
Registered: Apr 2003
Posts: 11

Original Poster
Rep: Reputation: 0
if i use sed -i..., it will erase my whole file. that is why i need to direct out to temp file first.
 
Old 04-20-2003, 11:53 PM   #12
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 50
Code:
 awk -F: '{print $1FS"*"FS$3FS$4FS$5FS$6FS$7FS$8FS}' shadow
 
Old 04-20-2003, 11:55 PM   #13
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 50
Re: Try your text editor

Quote:
Originally posted by jailbait
I have changed that field with my text editor, kwrite. Once I screw it up I usually use my rescue system command line text editor, mp, to fix it.
If you use vipw -s, it'll check syntax before writing and you won't have to worry about screwing up the syntax.
(man vipw)
 
Old 04-22-2003, 12:22 AM   #14
uribo
LQ Newbie
 
Registered: Apr 2003
Posts: 11

Original Poster
Rep: Reputation: 0
hi moses,

one question --> how can i update shadow file after insert "*" in "mark" field?

i tried:

cat /etc/shadow | awk -F: '{ if ($1 == "mark") print $1FS"*"FS$3FS$4FS$5FS$6FS$7FS$8FS}' > temp.doc && mv temp.doc /etc/shadow

& it replace the whole file content with only "mark" entry.

thanks
 
Old 04-22-2003, 09:35 AM   #15
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
Well as you can see above there are many ways to solve your problem. Here would be another way to do it:

sed -e "s/\(^shelly\):[^:]*/\1:*/" /etc/passwd > temp.doc && mv temp.doc /etc/passwd
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Divide up lines with string delimiter elmu Programming 3 10-07-2005 08:48 AM
getline delimiter + move to next line? blizunt7 Programming 3 07-09-2005 12:08 AM
c++ get() delimiter ashirazi Programming 3 08-06-2004 05:26 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
tab delimiter codename000 Programming 3 04-04-2003 10:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration