Using find

When not using ack, my favorite search tool, you can still exclude SVN folders. Here is an example of me looking for a file:

$ find . -not \( -name .svn -prune \) -name 'EosController*'
./web/models/controllers/EosController.php
./web/models/controllers/EosControllerTest.php

This mode of expression might be better for your scripting needs.

unix search-and-replace: rpl (and sed)

Evan showed the world his “weak Ruby script” to find-and-replace in multiple files, and was pointed to the amazing rpl utility.

Let’s see what the port maintainer has written about the rpl tool.


$ port info rpl
rpl 1.4.0, textproc/rpl (Variants: universal)

http://www.laffeycomputer.com/rpl.html

rpl is a Unix text replacement utility. It will replace strings with new
strings in multiple text files. It can scan directories recursively and
replace strings in all files found. The search can be limited to files with
certain filename suffixes (e.g. '.html', '.c', etc.).

Platforms: darwin
Maintainers: mich@freebsdcluster.org

Usage context: When you work on auto-setup of new projects (say, making your simplification pipeline even simpler) you can use rpl on configuration files, say, and have non-conflicting markers like MEGASKULLYOUR\_PROJECT_NAMEMEGASKULL which would be replaced by your configuration strings.

Or, when you find a new name for something, and just need to try out this tool

Update: got it to work with sed, which is everywhere. The machine I worked on had FreeBSD, but the port command didn’t answer.


  sed -e 's/max_preview_size = 262144/max_preview_size = 10000000/g' -i boook trac.ini

A breakdown, dear reader:

-e means add a command. s is for substitution of one sequence for another. g is for globally in given files, it could also be a number, say 1, meaning “replace match number 1″.

-i means “irritatingly in-place editing”

-i boook means “…and since it is irritating, add the backup file trac.ini.boook as it were before those edits, to not annoy more than necessary”

[tags]textprocessing,unix[/tags]

Snippet: remove .svn directories

Snippet to recursively drop Subversion control of a directory. It removes the .svn directories, and their contents.

find . -name ".svn" -exec rm -rf {} \;

Credit: Zed Shaw, at the Mongrel mailing list.

(Note to contemporary readers: Crazy old info) Oh, and if you are using Mongrel, and need help with it, there is a new chatroom at http://www.lingr.com/room/3yXhqKbfPy8, where Zed, the developer, hangs out.

[tags]unix,subversion,snippets[/tags]

bash functions example

Bradley Taylor (of [Railsmachine](http://railsmachine.com/) hosting fame) said this on [the excellent Mongrel mailing list](http://rubyforge.org/mailman/listinfo/mongrel-users):

“Just use these bash functions and put them in your .bashrc:”

  cluster\_restart () {
	mongrel\_rails cluster::restart -C /etc/ mongrel\_cluster/$1.yml;
  }
  cluster\_start () {
	mongrel\_rails cluster::start -C /etc/ mongrel\_cluster/$1.yml;
  }
  cluster\_stop () {
	mongrel\_rails cluster::stop -C /etc/mongrel\_cluster/$1.yml;
  }

  usage:
  $ cluster_start fluxura


([See the thread in the archives](http://rubyforge.org/pipermail/mongrel-users/2006-July/000780.html).)

That is, when someone asked about if it wasn’t possible to just have a command, and that it’d be cool if Mongrel had such a simple command, he shows code.

Also, the example’s great.

* The $1 to access the first argument to the function call, and that
* you don’t need to put in any parameter lists in the functions, and that
* its control structures looks mostly like any C-like language.

I feel kickstarted by this. Thanks, Bradley.

[tags]bash,unix[/tags]

Awk, text processing

Fooling around with load average output on a shared host (where it can be life-or-death):


$ w -s|awk '/load average:/ { print "Load averages:\nPast 1 min:  " $10 "\nPast 5 min:  " $11 "\nPast 15 min: " $12 }'
Load averages:
Past 1 min:  20.94,
Past 5 min:  20.67,
Past 15 min: 18.75

What’s w, you say? The manpage says:

> w – Show who is logged on and what they are doing.

Awk’s got grep rolled right into it. And the default action is “print the line”. Practical. (Above I had to go all fancy, and adding what those fields mean. Sorry about that. I’m like that.)

If you scroll a bit in the little code window above, you’ll see that the significant values are $10, $11, and $12. The tenth, eleventh, and twelvth *words*! If your data is tab-separated, you change the delimiter to a tab instead. Or, to a comma for CSV files. Bam, you can read almost anything right there.

The concatenation (fancy word for adding two strings to one) sign is… nothing. Elegant.

Read more on awk in [the gawk manual (Free documentation!)](http://www.cs.utah.edu/dept/old/texinfo/gawk/gawk_toc.html).

**Update**: Or, read esr’s [damning of awk](http://www.faqs.org/docs/artu/ch08s02.html#awk), in his [The Art of Unix Programming](http://www.faqs.org/docs/artu/).