Sarah
Sarah

Reputation: 133

Find and Replace text in .sql files

I have many .sql files in subfolders. I am presently manually opening them up, and searching for OLDSERVERNAME, and replacing it with NEWSERVERNAME(I'm doing migration). There must be a faster way to do this. I tried using FART, but I guess I wasn't doing it right.

This is what I tried(in main folder):

fart -i -p -c *.sql OLDSERVERNAME NEWSERVERNAME

Can I perhaps use unix utilities for this purpose?

Upvotes: 8

Views: 11441

Answers (2)

Ramya Ragupathy
Ramya Ragupathy

Reputation: 21

On MacOS - I had to add a backup file extension

sed -i '.bak''s/OLDSERVERNAME/NEWSERVERNAME/g' *.sql

Upvotes: 2

jaypal singh
jaypal singh

Reputation: 77105

You can use sed for this. sed stands for S tream Ed itor

sed -i 's/OLDSERVERNAME/NEWSERVERNAME/g' *.sql
  • -i option will do in-file substitution.
  • g implies global substitution. So if there are more than one instances of OLDSERVERNAME in one line they will get replaced with NEWSERVERNAME
  • *.sql will pass all files ending with .sql extension.

Look up sed man page for more details.

Upvotes: 14

Related Questions