Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 455
Sarah Avatar asked May 31 '13 03:05

Sarah


1 Answers

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.

like image 90
jaypal singh Avatar answered Sep 24 '22 15:09

jaypal singh