Unix Tricks for Fun and Profit
What can I say, I’m a stickler for good documentation. It’s one of the reasons I love OpenBSD so much. Anyway, here’s a few neat Unix (mostly sed) tricks I’ve used recently. Note that these are for BSD sed
, not GNU.
Grab text between a start and end point
Let’s say you have some text, and you only want text with specific tokens around it. sed
makes this trivial. I found this useful for parsing plaintext mail logs.
# The formula
sed -n '$START/,/$END/p' $FILE
# Example
> cat out
some text
begin
I want this text
stop
dont want this
> sed -n '/begin/,/stop/p' out
begin
I want this text
stop
Insert a line after a match
You must have two spaces before the a\
for this to work right on BSD sed
. Why? No clue
# The formula. Newline is required after the inserted line
sed -i '/$MATCH/ a\
$LINE\
' $FILE
# Example
> cat out
Header
match
line 1
line 2
> sed -i '/match.*/ a\
line 0\
' out
> cat out
Header
match
line 0
line 1
line 2
Double grep without grep
I usually choose grep
over awk
as it’s easier to use, but when you need to do multiple greps with logic, awk becomes the better tool
# The formulas
awk '/foo/ && /bar/' # prints lines that contain both 'foo' and 'bar'
awk '/foo/ && !/bar/' # prints lines that contain 'foo' but not 'bar'
awk '/foo/ || /bar/' # prints lines that contain either 'foo' or 'bar'
# Example
> cat out
hello
hello but also goodbye
goodbye
see ya!
> awk '/hello/ && /goodbye/'
hello but also goodbye
> awk '/hello/ && !/goodbye/'
hello
> awk '/hello/ || /goodbye/'
hello
hello but also goodbye
goodbye