filtering lines that begin with a numerical that has to be higher than a certain value.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Piping it through 'tail' will only show me the highest value, but for scripting purposes I would like to have it filtered.
The value of the first column should be numerically more than a certain value.
For bash, anything within single quotes is treated literally--no substitutions. Text within double-quotes is scanned for substitutions.
EDIT:
But I should point out that you'll need to protect the $1. Awk needs the $1 preserved for the script to work, but bash would want to substitute the first command line option for it. Hence, the backslash in front of the $, which should tell bash you want a literal $ and not a variable substitution.
Last edited by Dark_Helmet; 02-03-2012 at 03:24 AM.
Thanks to you I could now modify my "regtop"
I'm using this script on my linux systems and they are executed remotely by Zabbix.
I can enter the "regexp" in Zabbix and am able to extract valuable data from logs.
This is the code.
Maybe (and hopefully) you can use it too:
# cat /usr/local/sbin/regtop
Code:
#!/bin/bash
#####################################################
# regtop
#####################################################
# Uses logtail & readlink
# http://sourceforge.net/projects/logtail/
#####################################################
# echo 'zabbix ALL =(ALL) NOPASSWD: /usr/local/sbin/regtop' >>/etc/sudoers
#
# grep -iq 'UnsafeUserParameters' /etc/zabbix/zabbix_agentd.conf || sed -i -e 's/^Server.*/&\n\n# Allow regular expressions\nUnsafeUserParameters=1/' /etc/zabbix/zabbix_agentd.conf
#
# echo 'UserParameter=vfs.file.regtop[*], sudo /usr/local/sbin/regtop "$1" "$2" "$3" "$4" "$5" "$6"' >>/etc/zabbix/zabbix_agentd.conf
#####################################################
# 08-12-2010 by Frater
#
# The 4th parameter (minutes) is optional.
# When 0 or empty, it will use 'logtail' which only checks the portion which hasn't been parsed before
# When minutes is 1, it will take the whole file
# When it's greater than 1, it will use 'lastmins' that will only output the last x minutes
#####################################################
export PATH=${PATH}:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin
offset=/tmp/regtop.
v=
while getopts v name
do
case $name in
v) v='-v ';;
?) printf "Usage: %s: [-v] <file> <Eregexp>, [<Eregexp>] , [ <minutes> ]\n" $0
exit 2;;
esac
done
shift $(($OPTIND - 1))
[ ! -h "$1" ] && [ ! -f "$1" ] && exit 1
[ -z "$2" ] && exit 1
file2parse="$1"
ftmp1=`mktemp`
ftmp2=`mktemp`
minutes=`echo "$4" | awk -F. '{print $1}' | tr -cd '0-9'`
[ -z "${minutes}" ] && minutes=0
if [ ${minutes} -eq 0 ] ; then
fname="`readlink -f "$1"`"
expression="`echo "${fname}.${v}$2$3" | tr '/' '.' | tr -cd '.0-9A-Za-z-'`"
offset="${offset}${expression}.offset"
logtail -f "$1" -o $offset >${ftmp2}
file2parse=${ftmp2}
elif [ ${minutes} -gt 1 ] ; then
cat "$1" | lastmins ${minutes} >${ftmp2}
file2parse=${ftmp2}
fi
if [ ! -z "$3" ] ; then
ftmp3=`mktemp`
grep $v -E "$3" ${file2parse} >${ftmp3}
file2parse=${ftmp3}
fi
grep -oE "${2}" ${file2parse} | sort -o ${ftmp1}
total=`wc -l ${ftmp1} | awk '{print $1}'`
if [ $total -gt 0 ] ; then
rows=`echo "${5}" | tr -cd '0-9'`
[ -z "${rows}" ] && rows=5
expression="${2}"
[ -z "$3" ] || expression="${expression} && $3"
# get double the amount of rows you want to see (2 * 5 = 10)
uniq -c ${ftmp1} | sort -rn | head -n$((2 * ${rows})) >${ftmp2}
# calculate the average value of these lines
AVG=`cat ${ftmp2} | awk '{avg += $1}END{printf "%d\n", avg/NR}'`
# Only show the rows that have more than average, but no more than the amount of lines you wanted (default = 5)
head -n${rows} ${ftmp2} | awk "{ if (\$1 >= ${AVG}) print}"
echo "Total: $total lines with \"${expression}\""
else
echo '-'
fi
rm -f ${ftmp1} 2>/dev/null
rm -f ${ftmp2} 2>/dev/null
rm -f ${ftmp3} 2>/dev/null
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.