2

I'm trying to replace a.mysql.com in a file using sed for the following line

    'ENGINE': 'a.mysql.com', # MySQL host

How can I replace the text without removing the comment entry?

I tried the below but it isn't working

sed -i -e "s/\('ENGINE': ').*\([^']*\)/\1new.mysql.com\2/g" file.py
0

2 Answers 2

1

Following sed may help you in same.

sed "s/\(.*: \)\('.*'\)\(.*\)/\1'my_next_text'\3/"   Input_file

Output will be as follows.

'ENGINE': 'my_next_text', # MySQL host

EDIT: Tested it with edited Input_file of OP too as follows.

cat Input_file
    'ENGINE': 'a.mysql.com', # MySQL host

sed "s/\(.*: \)\('.*'\)\(.*\)/\1'my_next_text'\3/" Input_file
    'ENGINE': 'my_next_text', # MySQL host

EDIT2: IN case OP wants to check for line which has string ENGINE in it then following could be done.

sed "/    'ENGINE/s/\(.*: \)\('.*'\)\(.*\)/\1'my_next_text'\3/"   Input_file
Sign up to request clarification or add additional context in comments.

5 Comments

That gives me 'ENGINE': 'my_next_text'., not the expected output
@Anon, I tested my code before posting and it gave me proper output, could you please confirm if your Input_file is same as posted one?
@Anon, I have checked with your edited Input_file too and my code works fine in it too, could you please check and let me know. If you are still not seeing the correct output, please mention complete details about Input_file and expected output too in code tags and let us know on same.
I've updated my question, engine is a key in an object of objects with a tab space before it.
@Anon, in case you want to look for line which has string ENGINE in it and do the certain operations then my EDIT2 code may help you on same.
1

You can use the following logic using GNU sed to achieve your requirement.

sed "/ENGINE/s/'[^']*'/'new.mysql.com'/2" file 

The s/ENGINE/ matches any lines containing ENGINE and does the following substitution s/'[^']*'/'new.mysql.com'/2 which:

s/                # Substitute  
'                 # Match a single quote
[^']*             # Match anything not a single quote 
'                 # Match the closing single quote 
/                 # Replace with 
'new.mysql.com'   # The literal value
/2                # The 2 here matches the second quoted string, not the first. 

Add the -i extension once the file is modified appropriately.

3 Comments

That gives 'ENGINE': 'new.mysql.com'. not 'ENGINE': 'new.mysql.com', #MySQl host
@Anon: The answer was tested locally, it gives 'ENGINE': 'new.mysql.com', # MySQL host properly for me. Either you are running it wrong or having a separate input file than the one posted in the question
Yes, I checked. In my file ENGINE is a key in an object of objects, indented by a tab. Could you explain why adding a * to your answer, like sed -i "/*ENGINE/s/'[^']*'/'new.mysql.com'/2" common_settings.py doesn't work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.