grep -o does not work in Solaris

I am using the code below to grep through a list of files (TEMPFILE) and look for rsync, rdist, rsh, ftp, etc. in each file. Do a count of each, and output that to a logfile.

This works great in Linux, but not at all in Solaris because the EGREP -o option does not exist.

Anyone have an idea on how i can accomplish this in Solaris?

for FILENAME in `$CAT $TEMPFILE`
do
BASEFILENAME=`$BASENAME $FILENAME`
BASEDIR=`$DIRNAME $FILENAME`
SIZE=`$LS -l $FILENAME | $AWK -F" " '{print $5}'`
if [[ $BASEFILENAME != $SCRIPTNAME ]]
then
PROTOCOLS=`$EGREP -v '^[ |      ]*#' | $GGREP -o $INSECCOM $FILENAME | $XARGS`
if [[ -z $PROTOCOLS ]]
then
$ECHO "Do Nothing"
else
FTPCOUNT="0"
RCPCOUNT="0"
RSHCOUNT="0"
REMSHCOUNT="0"
RLOGINCOUNT="0"
TELNETCOUNT="0"
RSYNCCOUNT="0"
RDISTCOUNT="0"
for COMMAND in $PROTOCOLS
do
case "$COMMAND" in 
ftp) FTPCOUNT=`expr $FTPCOUNT + 1` ;;
rcp) RCPCOUNT=`expr $RCPCOUNT + 1` ;;
rsh) RSHCOUNT=`expr $RSHCOUNT + 1` ;;
remsh) REMSHCOUNT=`expr $REMSHCOUNT + 1` ;;
rlogin) RLOGINCOUNT=`expr $RLOGINCOUNT + 1` ;;
telnet) TELNETCOUNT=`expr $TELNETCOUNT + 1` ;;
rsync) RSYNCCOUNT=`expr $RSYNCCOUNT + 1` ;;
rdist) RDISTCOUNT=`expr $RDISTCOUNT + 1` ;;
esac
done

I've never used -o, but it looks like you're just trying to pull out some regex?

PROTOCOLS=`$EGREP -v '^[ |      ]*#' | sed 's/.*\(REGEX\).*/\1/'`

Looks like GGREP is installed on our systems.

I ended up with

`$EGREP -v '^[ |      ]*#' | $GGREP -Eo $INSECCOM $FILENAME | $XARGS`

1) Please do not post all over the place.
2) When you mention Operating Systems, please mention what version. There are umpteen versions of Solaris and HP-UX.
3) Please mention what Shell you are running. You code like you have a 15-year-old Shell.
4) If you have code which does not work, please explain in words what the code should do.
5) Why invoke commands with syntax like $EGREP? There is no reason to do this. The command is lower case egrep . There are a lot more examples in the script. $LS , $CAT , $AWK. Why?
6) What on earth is the value of the environment variable $GGREP ?

  1. I am trying to post the relevant items in the related forum. Should i just put everything in the Shell Scripting forum?
    2-4. I am coding for unknown OS. The reason for the $COMMAND is because at the top of my scripts i am doing uname to figure out which OS, then set my commands as variables based on the OS because Solaris5.10 do not put everything in the same place as HP-UX 11, in fact running which mount on hp-ux gets an error. You have to know where it is and set a variable or call it explicitly everytime, which is not an option. So im not coding for a 15yo shell, im coding for all shells...hence this gets complicated.
  2. On Solaris, GGREP is different than regular grep, hence a different variable is required.

You might be thinking...hmm why not just make a different script for each OS, that would be nice, if anyone at my company knew where these OS's were and what was running on them....but then again I guess that is why i still have a job :).

You could probably just properly set the PATH variable depending on uname output.