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
Anon
  • 845
  • 5
  • 30
  • 50

2 Answers2

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
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • That gives me 'ENGINE': 'my_next_text'., not the expected output – Anon Jan 03 '18 at 10:06
  • @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? – RavinderSingh13 Jan 03 '18 at 10:54
  • @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. – RavinderSingh13 Jan 03 '18 at 11:00
  • I've updated my question, engine is a key in an object of objects with a tab space before it. – Anon Jan 03 '18 at 11:20
  • @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. – RavinderSingh13 Jan 03 '18 at 11:26
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.

Inian
  • 80,270
  • 14
  • 142
  • 161
  • That gives 'ENGINE': 'new.mysql.com'. not 'ENGINE': 'new.mysql.com', #MySQl host – Anon Jan 03 '18 at 10:04
  • @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 – Inian Jan 03 '18 at 10:07
  • 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? – Anon Jan 03 '18 at 10:19