Flipkart

Wednesday, June 2, 2010

Find / Replace in a Text File

Sometimes you have a big file, like maybe a database export, and you need to do some find / replace on it... but it won't open in any of your text editors, because your machine runs out of memory trying to open it! Well, here's a way around that, by using the command-line and a little regular expressions.
The way this works is to output the contents of the SQL script (or whatever file you're using), pipe it through a program called "sed," which is specifically form manipulating streaming content, and then redirect that output into the new file. Sound complicated? Well... I guess it is a little, but the command itself looks simple!
  1. cat original_dump.sql | sed s/faruq/Shaik/ > new_dump.sql  
The new part here is really the "sed" program. Basically what it's doing is taking input and matching the first pattern (in this case, my name, "faruq"), and replacing it with the second pattern (in this case, a shortening of my name, "Shaik"), then outputting that.

No comments:

Post a Comment