Replace string with sed doesn't work


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace string with sed doesn't work
# 1  
Old 06-21-2010
Replace string with sed doesn't work

Hello,

Unfortunately I don't found any working solution for my problem :/

I have pass file for dovecot authorizing in this format:

Code:
user@domain.tld:{SSHA}Ykx2KVG/a2FKzjnctFFC2qFnrk9nvRmW:5000:5000::::
.
.
...

Now, I want to write some sh script for password changing for grep'ed user, in this case string: {SSHA}Ykx2KVG/a2FKzjnctFFC2qFnrk9nvRmW

Code:
#!/bin/sh

OLDPW=`grep user@domain.tld passfile | cut -d: -f2`
NEWPW=`dovecotpw -s ssha -p newpassword`

sed -e 's/$OLDPW/$NEWPW/g' passfile > tmpfile
mv tmpfile passfile

but sed command doesn't work. Output file is exactly the same as input. Generated password always starts with {SSHA} and often has special characters like "/" "+", and others...

OS: FreeBSD 8

Any help?... Smilie Thanks!

Last edited by vincenty; 06-21-2010 at 09:53 AM..
# 2  
Old 06-21-2010
First, you need to use double quotes here:
Code:
sed -e "s/$OLDPW/$NEWPW/g" passfile > tmpfile

Otherwise variables won't be expanded. Second thing is that you need to find character that is never used in generated password and use it as regex delimiter:
Code:
sed -e "s#$OLDPW#$NEWPW#g" passfile > tmpfile

Here "#" was used.
# 3  
Old 06-21-2010
Yes, it works now Smilie
Thank you very much! Smilie
# 4  
Old 06-21-2010
Be careful here. From what I'm seeing you're overwriting your passfile file with the changed passwords ONLY. This means anyone who's password wasn't changed is no longer in the passfile.

I'm not real fluent with FreeBSD but you might want to see if you can use ex. It's an inline vi editor. Your statement would look like this.

Code:
print 's/'$OLDPW'/'$NEWPW'/\nwq' | ex passfile

Some Unix flavors can use echo, others use print. You can test it very easily by creating a text file like so;
Quote:
line 1
line 2
line 3
this is a line
search string
Then from the command line use something like this.
Code:
# Insert a line of text after line 3 in a file.
print '3a\n'Inserted Line'\n.\n\nwq' | ex filename.txt

# Delete a line of text that contains the pattern "search string"
print 'g/'search string'/delete\nwq' | ex filename.txt

# Substitue all occurances of "this is a line" with "that is a line.
print '%s/'this is a line'/'that is a line'/g\nwq' | ex filename.txt

I find it quite handy for updating log / config / stat and many other types of files.
# 5  
Old 06-21-2010
Quote:
Originally Posted by mph
Be careful here. From what I'm seeing you're overwriting your passfile file with the changed passwords ONLY. This means anyone who's password wasn't changed is no longer in the passfile.
Well, sed command given by bartus11 solved my problem, and any other users in passfile who's password wasn't changed don't disappear.

Here a few lines from passfile (for dovecot auth)

Code:
user1@domain.tld:{SSHA}Hg+be1/EkLn4rmFNy+WFcaF9T2K9yWpv:5000:5000::::
user2@domain.tld:{SSHA}yTTllwdKqbQDyjBUpue9FTvyx+rW5zvx:5000:5000::::
user3@domain.tld:{SSHA}T/a56MosCHi4RFnZEhWC77Cl4EautRMQ:5000:5000::::
user4@domain.tld:{SSHA}mURhtdWsRl5C9/fLxmEu0LuuZ6ocRDrh:5000:5000::::
otheruser1@otherdomain.tld:{SSHA}Ykx2LE+fa2FKzjnctFFC2qFnrk9nvRmW:5000:5000::::
otheruser2@otherdomain.tld:{SSHA}ex67n9fe6BEtniu71xN1JrF4Yna8glLb:5000:5000::::

my script get parameter $1 from command line as user@domain.tld then grep line in passfile for this user password only

Code:
OLDPW=`grep $1 passfile | cut -d: -f2`

so now variable "OLDPW" keeps password only. Then variable "NEWPW" keeps new password generated by `dovecotpw` command, sed do replacement and writes changes do tmpfile, finally `mv` gives me a new passfile with new password for user@domain.tld only.

Anyway, thanks for your tips Smilie
# 6  
Old 06-22-2010
You're absolutely right. Sorry, I use ex instead of sed so much, I start thinking of them as interchangeable, but they're not.

I would have used something like this. Note: AFAIK, this only works under ksh.
Code:
#!/bin/ksh
print "%s/`grep $1 passfile | awk -F: '{print $2}'`/`dovecotpw -s ssha -p newpassword`/g\nwq" | ex passfile

In this case, had sed been used and dumped to a temp file, then moved back, the other users would have been lost.

I stand (actually, sit) corrected. Smilie

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

C shell concatenate string doesn't work

I have the following code: #!/bin/csh clear set cloud_file="/home/labs/koren/davidsr/general_scripts/MFP_10_PP_Parmas.txt" # to fill set mie_tables_dir='/home/labs/koren/davidsr/SHDOM_MAIN/MIE_TABLES/non_polo_wave_0.7_water_50R_s0.5_e25_max_70.mie' # to fill set prp_dir='${pp_dir}/pp_prp/'... (2 Replies)
Discussion started by: student_wiz
2 Replies

2. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

3. Shell Programming and Scripting

sed command works on Fedora/Ubuntu, but doesn't work in Mac

Hi, I have a question. I define a function using sed command: replace() { searchterm=$1 replaceterm=$2 sed -e "s/$searchterm/$replaceterm/ig" $3 > $WORK'tempfile.tmp' mv $WORK'tempfile.tmp' $3 } Then I call replace 'test = 0' 'test = 1' $myfile This code... (1 Reply)
Discussion started by: Dark2Bright
1 Replies

4. Shell Programming and Scripting

sed command works on Fedora/Ubuntu, but doesn't work in Mac

Hi, I have a question. I define a function using sed command: replace() { searchterm=$1 replaceterm=$2 sed -e "s/$searchterm/$replaceterm/ig" $3 > $WORK'tempfile.tmp' mv $WORK'tempfile.tmp' $3 } Then I call replace 'test = 0' 'test = 1' $myfileThis code works well in... (1 Reply)
Discussion started by: Dark2Bright
1 Replies

5. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

6. Shell Programming and Scripting

sed replace not work Pls. help me

sed replace not work Pls. help me I used sed command in my file 66875964560@1982589 90825890001@90825890001@3@15/12/2007 14:25:14@22/03/2010 6:06:13@20/01/2010 3:28:39 66873064490@1925912 90259120001@90259120001@5@02/04/2009 1:51:31@30/10/2009 3:08:34@13/09/2009 8:24:33... (3 Replies)
Discussion started by: ooilinlove
3 Replies

7. Shell Programming and Scripting

Does Sed Search/Replace Work For Multiple Matches On The Same Line?

Hello, I would like to delete all the footnotes in all my htm files. Hence, I have to delete the whole font tag pairs, i.e. deleting everything between the begin/end font tags. I create a testfile, of which data parts of all four lines are the same except for the number of font tag pairs,... (3 Replies)
Discussion started by: cibalo
3 Replies

8. Shell Programming and Scripting

SED - replace with new line didn´t work for solaris

Hi This is what I was trying to do, comment one line and add something different in a new line right next. This is the command I want to do more .profile | sed 's,STRING1, #STRING1 NEWLINE STRING2,' (I´m using ',' because my string is something like this exec... (3 Replies)
Discussion started by: alcalina
3 Replies

9. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

10. Shell Programming and Scripting

sed doesn't work

Hello I' m confused a bit. I want to replace string "&amp" with "&" using this command. sed 's/&amp/&/g' and it doesn't work. Nothing happens. On the other side this works: sed 's/&amp/@/g' or sed 's/&amp/^/g' !!! Can somebody help please? Thanks (3 Replies)
Discussion started by: billy5
3 Replies
Login or Register to Ask a Question