Tuesday, May 15, 2007

Awk and Textmate - search and replace for LARGE files

Textmate is great, but it tends to choke when you do a search / replace operation on really large files. When it does not choke, it just takes a REALLY long time. Awk to the rescue.

A common task (at least for me) is to update all the fields of a certain type in Rails fixture file. We have one for vehicle records that has around 1500 records, each averaging around 46 lines, for a total file length of around 69K lines. I tried replacing one field for each record (using a regex) in this file, and Textmate ground to a halt.

Enter awk. Using Textmate's Filter Through Command, I performed the operation with one awk command. It took 3 seconds to complete.

Here's how:
1) open the file to be processed in Textmate
2) Hit Command-Option R to invoke Filter Through Command...
3) Enter your awk command, WITHOUT the filename. Mine was

awk '{ sub(/towed_at_date:.*/,"towed_at_date: <%= Date.today %>"); print }' 


4) Select Input: Document, and Output: Replace Document in the dialog box
5) Hit execute.

1 comment:

Velma said...

Well written article.