Daemian Mack home

Printing only the matching column with awk and grep

October 6, 2011

I’ve spent way too many minutes looking this up over the years.

awk 'match($0,/regexp/) {print substr($0,RSTART,RLENGTH)}' inputfile

and about 1 time in 10 I’m startled to remember grep makes this very easy…

grep -o regexp inputfile

From here.