johnsu01 (johnsu01) wrote,

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
  • Post a new comment


    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.