sed.

Kasyx

Expert Member
Joined
Jun 6, 2006
Messages
2,565
Reaction score
1
Location
127.0.0.1
I'm having a bit of trouble with sed at the moment. I am quite sure it is because I am a dumbass and have forgotten how to handle my quotes and regular expressions.

Basically, I am wanting to use sed in a script to replace a line defining a mysql database password. In theory it should be working, here is how I have been testing it (I just cat the file and pipe it to sed, which should then display the substituted line:

Code:
cat /home/kasyx/mysqldb.txt | sed 's/my($db_pass) = '';/my($db_pass) - 'password';'

That, I would imagine, is what it should look like, but it just doesn't work :( I think the single quotes are causing all the hassle. I tried surrounding it with double quotes (""), but that pretty much achieved nothing. I have also tried using backslashes to define the single quotes within the string. Also no luck.

If anyone has any thoughts, suggestions or another way to do this, please help me before I go insane and kill someone.
 
If I may ask, what does the line you are trying to match in mysqldb.txt look like?

Note that the general search and replace syntax for sed is

Code:
sed 's/search regex/replace string/;'

Remember to escape any characters with a backslash that are reserved or repeat sed syntax and remember to terminate with a semi-colon.

If you wanted to replace
Code:
my($db_pass) = ''
with
Code:
my($db_pass) = 'password'
then something line this?:

Code:
$ echo "my(\$db_pass) = ''" | sed "s/\(my(\$db_pass)\) = ''/\1 = 'password'/;"
my($db_pass) = 'password'
 
Last edited:
Oookay :o

wmaker said:
$ echo "my(\$db_pass) = ''" | sed 's/\(my(\$db_pass)\) = ../\1 = 'password'/;'
my($db_pass) = password

Would you mind explaining to me what exactly you did there? It could be that my brain is mush from my Resident Evil 4 marathon last night, or that I am just stupid, but I don't quite understand the theory behind it.
 
I wasn't sure what you have in your txt file so I echoed out : my($db_pass) = ''
That is the part before the pipe. Then sed replaced the space between the single quotes with the word: password. If this is what you wanted to do and have in a line in your txt file, then this sed argument works.

As for the syntax, if you group in a regex for sed, you need to escape the group brackets with a backslash. You can then print out your group again by its order number. In this case there was only 1 group so it is printed out as \1. This is standard search-and-replace stuff. Elsewhere (e.g. in vi, perl, python, emacs, etc.) grouping and escaping in a regex may use slightly different syntax.

It is not necessary to do it the way I did it - the grouping is just a way to find the search string and then not rewrite it in the replace part. Also, this sed string is probably not even the best one can do. If you have to allow for variable spaces left and right of the equals sign or for text before and after your search string, it needs to change.

Remember the saying:

Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.

:D
 
Okay, tested that out. It works when piping echo to sed, however it doesn't work when piping cat, which, unfortunately is what I need. I think the issue lies in the $ in the original string I am trying to replace. It can be escaped with a backslash when issued via echo, however not I cat it from a file. Any clue how to sort that out?

Thanks for all the help so far :)
 
Remember to escape the $ in your sed script:

Code:
$ echo "my(\$db_pass) = ''" > db.txt

$ cat db.txt
my($db_pass) = ''

$ cat db.txt  | sed "s/\(my(\$db_pass)\) = ''/\1 = 'password'/;"
my($db_pass) = 'password'
 
The problem is that I did not create the file I am trying to sed. I am creating an automated MailScanner/mailwatch install script and I need to sed two of the files to automatically set the mysql password information. This means that I can't escape the backslash in the original file as it is part of a tarball :(
 
The dollar sign is not escaped in the sample file:

Code:
$ cat db.txt
my($db_pass) = ''
 
Top
Sign up to the MyBroadband newsletter
X