Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Using Grep within a UNIX Script Post 302110385 by sb008 on Monday 12th of March 2007 05:31:45 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 06:07 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy