johnsu01 (johnsu01) wrote,
johnsu01
johnsu01

sed one-liner to replace based on grep results

I used this to replace a mistake in the address at work that was made in dozens of files. It replaced every occurrence of " 9 Temple Place" with " 59 Temple Place", recursively.

sed -i 's/\ 9\ Temple\ Place/\ 59\ Temple\ Place/g' $(grep -lr " 9 Temple Place" ./*)

If you wanted to play it safe and make a backup of all the files that were changed, you would replace "-i" with "-i.bak" or whatever suffix you like to use for backups.

Tags: bash, grep, sed
Subscribe
  • Post a new comment

    Error

    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments