grep -o does not work in Solaris


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep -o does not work in Solaris
# 1  
Old 07-30-2012
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?

Code:
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

# 2  
Old 07-30-2012
I've never used -o, but it looks like you're just trying to pull out some regex?

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

# 3  
Old 07-31-2012
Looks like GGREP is installed on our systems.

I ended up with

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


Last edited by methyl; 07-31-2012 at 06:21 PM..
# 4  
Old 07-31-2012
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 ?

Last edited by methyl; 07-31-2012 at 07:06 PM..
# 5  
Old 07-31-2012
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.
5. 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 Smilie.
# 6  
Old 07-31-2012
You could probably just properly set the PATH variable depending on uname output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep doesn't work when assigning to variable

Hello, First of all, I'd like to say hello to all members of forum. Can You please help me with the matter described below? I am trying to fetch a data from the file to variable, I am doing this using below script: returned=`tail -50 SapLogs.log | grep -i -E "Error|"` echo $returned ... (2 Replies)
Discussion started by: jedzio
2 Replies

2. UNIX for Dummies Questions & Answers

grep command does not work

Hello. I have a question as to why my grep command does not seem to be working. grep -c '^?*' ~ grep -c '^.*' ~ I wanted to count the number of files starting with the letter K and have some letters after that. I have such files in my home directory. However, the output is 0. So, I have no... (2 Replies)
Discussion started by: FUTURE_EINSTEIN
2 Replies

3. Shell Programming and Scripting

Cannot get grep to work within function.

Hello again, Am having an issue now with getting a simple grep command to work within a function.. The function is as below... function findRecord() { output=grep "001" recordDatabase echo $output } At the moment the "001"... (3 Replies)
Discussion started by: U_C_Dispatj
3 Replies

4. Solaris

Creating script - grep wont work?

This is my first script and I wont get it working.. sorry for being a total noob but here it is: #./bin/sh -x echo "1:st argument = $1"; echo "2:nd argument = $2"; grep "$1" "$2" In the terminal I write, for example, su.sh sausage sausage.txt Also tried su.sh "sausage" "sausage.txt" but... (2 Replies)
Discussion started by: Prantare
2 Replies

5. Shell Programming and Scripting

tail -XXX with grep doesn't work in while loop

Hi all, I need some help. my shell script doesn't work especially in the loop. #!/bin/sh -xv export ORA_ADMIN=/oracle/home/admin export ORACLE_SID=ORA_SID cat ${ORA_ADMIN}/param_alert_log.ora | while read MSG do #echo $MSG #echo "tail -400... (8 Replies)
Discussion started by: sidobre
8 Replies

6. Shell Programming and Scripting

two grep in one script doesn't work?

Hi there, the following script doesn't work. the first part works, then the second 'grep' fails with ': not found'. However, if I take out the second part (starting with the grep command) and put in a seperate script, it works. everyone know what's wrong here? no two 'grep' in one script, that... (2 Replies)
Discussion started by: monkey77
2 Replies

7. UNIX for Dummies Questions & Answers

For some reason, my grep doesn't work as expected

I am trying to find only those entries where 7018 and another number appear in the end of the line. 7018 2828 1423 2351 7018 2828 14887 2828 7018 1222 123 7018 1487 I am looking for a way to generate only the last two lines. I was trying to do just "grep '7018{1,5}" but it does not... (5 Replies)
Discussion started by: Legend986
5 Replies

8. Solaris

grep -e doesn't work on solaris

grep -e doesn't work in Soalris. Same script with grep -e worked on AIX/HP/LINUX.. I would like to search a list of patterns on "log.txt" like ... grep -e FATAL -e ERROR log.txt I get the error message as grep: illegal option -- e Usage: grep -hblcnsviw pattern file . . . (3 Replies)
Discussion started by: jmkraja
3 Replies

9. UNIX for Dummies Questions & Answers

Why does this grep not work?

Hi Could you please tell me why the following grep does not match the string? > echo ' 1661 Rows not loaded because all WHEN clauses were failed.' | grep 'Row* not*WHEN*' Thanks gnmike (2 Replies)
Discussion started by: GNMIKE
2 Replies

10. Shell Programming and Scripting

grep doesn't work within shell script?

I am trying to run the following code from a script file but it complains that syntax of (both instances of) grep is wrong. When I copy and paste it to the terminal, it is OK. Any idea what the problem might be? set i = `grep -c #define flags.h` while ($i>20) @ i-- my func (`cat... (4 Replies)
Discussion started by: barisgultekin
4 Replies
Login or Register to Ask a Question