I need to modify a file in a Docker image. I would like to change the line 2357 of a Python module mod.py
with sed, how can I do that ?
raise ExchangeError(self.id + ' ' + errorText + '(#' + errorCode + ')')
The desired output would be:
raise ExchangeError(self.id + '(#' + errorCode + ')')
or
raise ExchangeError(self.id)
3
Answers
You can use the following sed command to replace content on a specific line:
Where:
You might have to play around with escaping special characters.
If the line you want to edit is unique in the code, try the following.
sed -i 's|raise ExchangeError(self.id + ' ' + errorText + '(#' + errorCode + ')') .*|raise ExchangeError(self.id + '(#' + errorCode + ')')|' mod.py
It would search for the exact line and replace it.
The cleaner syntax is as follows:
Try it with: