Register
It is currently Wed Apr 16, 2014 9:51 pm

Paragraph grep


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Thu Dec 20, 2012 5:43 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 223
Location: London - UK
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
}


Top
 Profile  
 PostPosted: Wed Nov 06, 2013 5:11 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
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
...


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP