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
--------------------------
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.
"${?}" 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.
By adding the "| wc -l" the number of occurences is counted.
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.
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:
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)