BashScripts.org
http://bashscripts.org/forum/

Paragraph grep
http://bashscripts.org/forum/viewtopic.php?f=16&t=1678
Page 1 of 1

Author:  DarthWavy [ Thu Dec 20, 2012 5:43 am ]
Post subject:  Paragraph grep

Yarg

Here's my paragraph grep, feel free to suggest improvments O-)

Code:
# paragraph grep
function para {
  if [ "$1" == "" ]
  then
    echo "Usage: para <pattern> [ <filename> ]"
  else
    if [ "$2" == "" ]
    then
      perl -00 -ne "print if /$1/"
    else
      perl -00 -ne "print if /$1/" < "$2"
    fi
  fi
}

Author:  jsz [ Wed Nov 06, 2013 5:11 pm ]
Post subject:  Re: Paragraph grep

Code:
paragrep() {
    [[ ${2+_} || ! -t 0 ]] &&
        sed '/./{H;$!d;};x;\'$'\34'"${1/$'\34'}"$'\34''!d' "${2-/dev/stdin}"
}

With that trick you're not restricted to pass it only a file.
You can use it in different manners:
Code:
paragrep pattern file
paragrep pattern < file
paragrep pattern <<< string
echo string | paragrep pattern
...

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com