Unix Tricks for Fun and Profit

What can I say, I’m a stickler for good documentation. Anyway, here’s a few neat Unix (mostly sed) tricks I’ve learned recently. Note that these are for BSD sed, not GNU and as such are only tested on OpenBSD and MacOS:

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

# 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 grep for multiple things in a line and don’t want to pipe grep into grep, here’s a quick trick

# 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/' out
hello but also goodbye

> awk '/hello/ && !/goodbye/' out 
hello

> awk '/hello/ || /goodbye/' out 
hello
hello but also goodbye
goodbye

fputs.com

How did I get here I am not good with computer


2021-10-23