Skip to content

Instantly share code, notes, and snippets.

@maoshuai
Created January 23, 2019 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maoshuai/33113ac457aca7869171942c696f46d3 to your computer and use it in GitHub Desktop.
Save maoshuai/33113ac457aca7869171942c696f46d3 to your computer and use it in GitHub Desktop.
full_grep()
{
# keyword to search
local keyword=$1
# mark the actual new line, in this job, it is always a date
local newLinePattern="^[0-9]{2}\/[0-9]{2}\/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}"
cat | awk '
BEGIN{
isFound = "no"
}
# match lines with keyword
{
# if match print the line
if($0~/'"$keyword"'/)
{
print $0
isFound="yes"
}
# if new line begin, flush the flag
else if($0~/'"$newLinePattern"'/)
{
isFound="no"
}
# if isFound, print continuely
else if(isFound=="yes")
{
print $0
}
}
'
}
full_grep $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment