Skip to content

Instantly share code, notes, and snippets.

@abhi1010
Last active August 29, 2015 14:01
Show Gist options
  • Save abhi1010/924a5f12f3067ba0b3af to your computer and use it in GitHub Desktop.
Save abhi1010/924a5f12f3067ba0b3af to your computer and use it in GitHub Desktop.
Print out sections
# Useful command to get the group of log lines that:
# - Start with a keyword
# - End with a keyword
# Works on gzip files as well
# Breaks all groups by printing ---- in between as well.
# Note: Only works well when you have a well defined begin and end keyword else everything might be printed out
group() #Usage = 'group filename printBeginKeyword printUntilKeyword NumOfLines' // Works with regex
{
if [[ "$#" -lt 3 ]]
then
echo ...... Number of arguments are not enough. Please try again..
echo _____ Usage=\"group FileName PrintBeginKeyword PrintUntilKeyword NumOfLines\[Optional,default=50\]\"
echo _____ Example=group tmp.log wotDA5 op_if_sell_c 55
return
fi
NUM_LINES=50
if [[ "$#" -gt 3 ]]
then
NUM_LINES=$4
fi
VERBOSE=1
if [ $VERBOSE -eq 1 ]
then
echo Begin Section=$2
echo End Section=$3
echo File=$1
echo NUM of lines=$NUM_LINES
fi
if [[ $1 == *.gz ]]
then
GREP_RESULT='gunzip -c $1 | grep -A $NUM_LINES $2'
else
GREP_RESULT='grep -A $NUM_LINES $2 $1'
fi
eval $GREP_RESULT | awk 'BEGIN { TO_PRINT=0 } \
/'$2'/ { TO_PRINT=1; } \
/'$3'/ { if (TO_PRINT==1) print $0 "\n------------------------" ; TO_PRINT=0; } \
( TO_PRINT==1 ) { print $0 } '
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment