User Tools

Site Tools


linux:linux_grepp

Grep by paragraph instead of by line

Available natively on AIX implemented in grep, the option '-p' can be implemented on linux.

Grep by paragraph instead of by line.

grepp() { [ $# -eq 1 ] && perl -00ne "print if /$1/i" || perl -00ne "print if /$1/i" < "$2";}

Something like this should operate the similar to “grep –color=auto”:

grepp() { [ $# -eq 1 ] && perl -00ne 'if ( /'$1'/i ){my $s = $_;$s =~ s/'$1'/\033[1;31m$&\033[0m/g; print $s}' || perl -00ne 'if ( /'$1'/i ){my $s = $_;$s =~ s/'$1'/\033[1;31m$&\033[0m/g; print $s}' < "$2";}

I just discovered that it fails if there are spaces in the regex. Quoting the regex fixes it:

grepp() { [ $# -eq 1 ] && perl -00ne 'if ( /'"$1"'/i ){$s = $_;$s =~ s/'"$1"'/\033[1;31m$&\033[0m/g; print $s}' || perl -00ne 'if ( /'"$1"'/i ){$s = $_;$s =~ s/'"$1"'/\033[1;31m$&\033[0m/g; print $s}' < "$2";}

Also

grepp() { x=$1; shift; perl -00ne ' print if /'"$x"'/i ' "$*" ; }
linux/linux_grepp.txt · Last modified: 2021/01/01 21:25 (external edit)