Using Grep within a UNIX Script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using Grep within a UNIX Script
# 1  
Old 03-12-2007
Using Grep within a UNIX Script

Hi,

I'm trying to save the wc from a grep command in my unix script and then later in my script I try to reference the variable but I have no luck. Any help is greatly appreciated!

--------------------

set xTest = 'grep -i lisa daily.log'

if [$xTest == 0] ;
then
echo "TEST" >> daily.log
fi
--------------------------


Thanks,
Lisa
# 2  
Old 03-12-2007
Quote:
Originally Posted by lisa_0801
Hi,

I'm trying to save the wc from a grep command in my unix script and then later in my script I try to reference the variable but I have no luck. Any help is greatly appreciated!

--------------------

set xTest = 'grep -i lisa daily.log'

if [$xTest == 0] ;
then
echo "TEST" >> daily.log
fi
--------------------------


Thanks,
Lisa
"grep i lisa daily.log" does not return the a count. It returns all ines in the file "daily.log" which contain the string "lisa" (case insensative).

If you are only interested if the string "lisa" does occur in the file you can use this command.

However you will have to check on the return code. If 0 the string "lisa" was found, if "1" the string was not found.

Furthermore, the "==" in the if condition is to compare strings, and not to check if a variable has a specific integer value. To check for integer values you use "-eq", "-ne", "-gt", "-ge", "-lt" or "-le".

Just to check if the srting occurs in the file you can use the following script.

Code:
#!/usr/bin/ksh

xTest = 'grep -i lisa daily.log'

if [ ${?} -ne 0 ] 
then
  echo "TEST" >> daily.log
fi

"${?}" is a reference to the return code of the command "grep".

Since you want to add the line "TEST" to the file if the string "lisa" did not occur in the file, it means the return code of grep has to be NON 0 (grep didn't find the string).

If you want to work with the number of times the string occurs you can use the following script.

Code:
#!/usr/bin/ksh

typeset -i xTest
xTest = 'grep -i lisa daily.log | wc -l'

if [ ${xTest} -eq 0 ] 
then
  echo "TEST" >> daily.log
fi

By adding the "| wc -l" the number of occurences is counted.
# 3  
Old 03-12-2007
sb008 you've replicated one error...spaces around the "="
# 4  
Old 03-12-2007
Quote:
Originally Posted by reborg
sb008 you've replicated one error...spaces around the "="
You are correct
# 5  
Old 03-12-2007
Alternative syntax...
Code:
if ! grep -qi lisa daily.log
then
   echo not found
fi

# 6  
Old 04-18-2008
Question

Quote:
Originally Posted by sb008
Since you want to add the line "TEST" to the file if the string "lisa" did not occur in the file, it means the return code of grep has to be NON 0 (grep didn't find the string).

If you want to work with the number of times the string occurs you can use the following script.

Code:
#!/usr/bin/ksh

typeset -i xTest
xTest = 'grep -i lisa daily.log | wc -l'

if [ ${xTest} -eq 0 ] 
then
  echo "TEST" >> daily.log
fi

By adding the "| wc -l" the number of occurences is counted.
Hi,

I am doing something very similar to this, only with two scripts - one main script and this little function in another script. The main script calls this grep function multiple times, looking for the word "Error". It gets as far as the grep statement, and then exits the whole program. X is the variable I used to store the result of the grep.

The grep script:
Code:
ls -lt $DEFDIR/outfile
if [ -s $DEFDIR/outfile ]
then
   x=`grep -ci 'Error' $DEFDIR/outfile`
   if [ ${x} -eq 0 ]
   then                       
            echo 'Process successful'
            echo "for Input data $INPUT_FILE"
   else                                             
            echo 'Error in file $INPUT_FILE' 
   fi
fi

So, my standard list for my main script ends up looking like:

+ cd <directory the scripts are in>
+ <execute grep script call>
+ <list the file outfile>
+ [ -s outfile ]
+ + grep -ci Error outfile
x=0
+ echo logout
logout

Script completed with exit code 1

The grep should echo "Process completed successfully" if grep didn't find "Error", but instead it just exits the whole script. I should note that this script used to execute fine until I added the shell specification code #!/bin/ksh -e. I know that the -e forces the script to exit if it finds an error, but if the grep statement passes I do not see where the error could be.

Thanks ahead of time for all your help!
# 7  
Old 04-21-2008
Does anyone have any ideas as to what I could try next, or what this could possibly be?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Unix grep in perl script

Hello, Fairly simple really I have an xml file and I want to check to see if it contains a pattern. The pattern is "../" On the command line I can type: grep "\.\./" myFile.xml and I get desired result. To do the same thing in a perl script I thought it was as simple as putting the ``... (4 Replies)
Discussion started by: Jaymoney
4 Replies

2. Shell Programming and Scripting

Unix Script Help - to grep an output and compute

Hi guys, I'm not a frequent visitor of this site so please forgive if this has been asked before. I'm trying to monitor the swap memory usage on my Solaris 10 Unix server. The command I'm wanting to run is: swap -s The output is: total: 33332016k bytes allocated + 40373832k... (2 Replies)
Discussion started by: d-train
2 Replies

3. Shell Programming and Scripting

New Unix user with shell script question using grep

Hello, I am a new Unix user and new to shell programming. I am working on a script to go through a log file and find the text error: grep -i 'error' monplus.mplog if I find the text error in the log file I would like to echo a message to the operator staing there is an error I am currently... (2 Replies)
Discussion started by: dtracy01
2 Replies

4. Shell Programming and Scripting

script use min resource ( grep grep)

Hi i wrote script use it as watchdog ( i mean it check another program (pooya) whenever that was killed (closed or crashed) it run another script (pooya_start.sh) to start it, this script work fine and do the job for me , i need help of an expert to tell me (exact command) how to change this... (8 Replies)
Discussion started by: pooyair
8 Replies

5. Shell Programming and Scripting

Unix Script To Move Files Based On Grep

I am looking for advice on how to write a script that will rename and/or move files to a different directory based upon the results of a grep. Let's say I have ten files in a directory. Some of them - not all - contain the text 'HELLO'. I would like to be able to grep the files for that text,... (3 Replies)
Discussion started by: rjhjr64
3 Replies

6. UNIX for Dummies Questions & Answers

| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax

Hello, I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide. ls -aLl /bin | grep "\(x\)" Which works, just highlights 'x' where ever, when ever. I'm trying to to get (?:) to work but... (4 Replies)
Discussion started by: MykC
4 Replies

7. UNIX for Dummies Questions & Answers

| help | unix | grep - Can I use grep to return a string with exactly n matches?

Hello, I looking to use grep to return a string with exactly n matches. I'm building off this: ls -aLl /bin | grep '^.\{9\}x' | tr -s ' ' -rwxr-xr-x 1 root root 632816 Nov 25 2008 vi -rwxr-xr-x 1 root root 632816 Nov 25 2008 view -rwxr-xr-x 1 root root 16008 May 25 2008... (7 Replies)
Discussion started by: MykC
7 Replies

8. Shell Programming and Scripting

unix grep

Hola, solicito ayuda para procesar un archivo de texto como el siguiente y luego obtener las variables. archivo.txt: Sending 5, 100-byte ICMP Echos to 10.249.98.94, timeout is 2 seconds: Success rate is 100 percent (5/5), round-trip min/avg/max = 28/33/44 ms Variables a obtener: ... (5 Replies)
Discussion started by: mmunarriz
5 Replies

9. Shell Programming and Scripting

Unix Shell Script Help - Grep

Hi, I am new to UNIX Shell scripting, and will require some help. I am trying to search a directory with a number of files for a specific word, then if that word is found in any of the files, then the filename is appended to a log file or result file by ">" command. I have been trying to... (2 Replies)
Discussion started by: Sypherg
2 Replies

10. Shell Programming and Scripting

Unix shell script (grep -A 6 -B 2 "ORA-" filename

BACKGROUND: I am using Solaris 10. Some of my boxes have gnu grep and I can use -A and -B flags on those. However, the solaris flavor of grep won't use the flags -A or -B. And some of my boxes won't be getting gnu grep. Should I try using perl, awk, or sed? Actual PROBLEM: I am... (7 Replies)
Discussion started by: el_guero
7 Replies
Login or Register to Ask a Question