August 24, 2005

Pipe programming

This page finally pushed me from using one command at a time to being comfortable constructing long strings of commands. E.g. cvs diff | grep Index: | grep -vi '*.inc' | sed 's/Index: //g' | xargs cvs commit -m "comment about change" > temp.txt, which lists the changes I've made, extracts the paths of the changed files, removes .inc files from that list, removes some extra chaff from the original printout, commits the changed files, and records the output in temp.txt.

Key commands to learn, IMHO, are find, grep, sed, and xargs. Plus man and apropos, of course.

Perl regexes were based in part on grep and sed, but I learned the Perl version first and now find myself applying that knowledge to grep and sed. Weird, huh?


At August 24, 2005, Blogger Fraxas said...

Wow. you're a brave man, committing to cvs in a find. I trust you piped the list to `xargs echo` before doing a commit?

And if you're interested in a slightly different toolchain, find has a verb that's isomorphic to xargs: -exec.

-exec works like this:

-exec [foo] {} [bar] \;

where [foo] and [bar] are your actual command, and {} takes on the value of each file that matches find's other criteria. Also, sed has other commands than s///; if you're deleting something, you can say d//[gi] to delete.

next step: learn awk! Awk is great, for value of great approaching heinous.

At August 24, 2005, Blogger JeremyHussell said...

Well, it was either that or commit 700+ files by hand. We had just changed the format of our <title> tags, you see.

I did check to make sure I wasn't doing something stupid first, and everything worked flawlessly.


Post a Comment

<< Home