I have a file containing my source code for a project I'm working on at the moment.

I have the word resolution scattered over the file, and want to prefix it with self.


resolution = readString[:16]
resolution = resolution.replace("\"", "")


How can I do this?

Python, sed, grep, awk, anything...

  • In Idle, simply use Ctrl+H – Jacob Vlijm Jan 27 '16 at 21:54

Using sed:

sed -i 's/resolution/self.&/g' some/file

Though perhaps you should start using an IDE and its refactoring tools.

If resolution could be part of word (say you had resolutionFoo), use word boundary markers:

sed -i 's/\bresolution\b/self.&/g' some/file

You can use Vim in Ex mode:

ex -sc '%s/resolution/self.&/g|x' file
  1. % select all lines

  2. s substitute

  3. g replace all instances in each line

  4. x save and close

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.