Monday, 12 January 2009

sed Magic: Removing Lines

Problem: Remove lines from a file f2 if they contain a word in file f1.

for word in `cat f1`; do
sed -i "/$word/d" f2;
sed is a stream editor, the -i option performs the editing in-place (i.e. in the original file).

This came up in the office today and I thought the solution was rather neat (if equally straightforward). Alternatives in most scripting languages (except maybe Perl?) are longer and/or uglier.

No comments:

Post a Comment