grep to handle a 0 result


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep to handle a 0 result
# 1  
Old 02-05-2007
grep to handle a 0 result

Hi guys,

I have the following grep command in a script to search through a file for a string and return its count, and it works fine for when the string exists:

grep "string" file.txt | wc

However, sometimes the result will be 0 and I want the script to take this as the result. Right now it is just dumping out with a "child process exited abnormally"

How can I get around this so that if the result is 0 then the script doesnt dump out and takes the result as 0?

thanks
# 2  
Old 02-05-2007
Code:
grep -c 'string' file.txt

# 3  
Old 02-05-2007
Can you please post your script here? Let us see what exactly you are trying to acheive.
# 4  
Old 02-05-2007
I have tried grep -c but that doesnt work either, it keeps on dumping out.

The script is actually a TCL script with embedded UNIX commands. The part its failing on is like so:

set td [exec grep -c "InvalidMessage" $logdir/$t | wc -l]

(I have assigned the variables logdir and t from before)

and i run the script and this occurs:

child process exited abnormally
while executing
"exec grep -c "InvalidMessage" $logdir/$t | wc -l"


It happens because there are no "InvalidMessage" strings in the file, but I know this and want the result i.e. 0 to be assigned to td
# 5  
Old 02-05-2007
grep -c "string" also gives you the count of lines then why are you still piping the output to wc -l, it'll always give you the output as "1", please remove it and try.
# 6  
Old 02-05-2007
Hi,

I removed the wc -l as well. That too didnt work and the result kept on dumping out again...

note: The double quotes i have used are correct in this instance, as this is how a UNIX command is executed in TCL
# 7  
Old 02-05-2007
Quote:
Originally Posted by ocelot
Hi,

I removed the wc -l as well. That too didnt work and the result kept on dumping out again...

note: The double quotes i have used are correct in this instance, as this is how a UNIX command is executed in TCL
See this link: http://wiki.tcl.tk/8489

Regards,
Tayyab
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep result from dd command

Hi, I am running following command in a bash script for testing IO and use grep to get throughput number, but it did not work, it displayed everything: dd if=/dev/zero of=/dev/null bs=1G count=1 oflag=dsync | grep bytes | awk '{print $7}' 1+0 records in 1+0 records out 536870912 bytes... (2 Replies)
Discussion started by: hce
2 Replies

2. UNIX for Dummies Questions & Answers

Bash - CLI - grep - Passing result to grep through pipe

Hello. I want to get all modules which are loaded and which name are exactly 2 characters long and not more than 2 characters and begin with "nv" lsmod | (e)grep '^nv???????????? I want to get all modules which are loaded and which name begin with "nv" and are 2 to 7 characters long ... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

How to handle Numerical result out of range?

i have this code: a=hello999999999999999999999999999999999999999999999999999999999 b=`echo ${a} | tr -d ''` if ; then echo "Zero" fi but when I execute this I am having this error: Numerical result out of range Anyone know how to handle this? Thanks!! (4 Replies)
Discussion started by: h0ujun
4 Replies

4. UNIX for Dummies Questions & Answers

grep, expecting 1 result, getting more

Hi Please take a look below, I'm grepping for /app/oracle and would like explicitly that result and not /app/oracle/admin as well. # cat /tmp/fs.list /app/oracle /app/oracle/admin # cat /tmp/fs.list | grep -w "/app/oracle" /app/oracle /app/oracle/admin (3 Replies)
Discussion started by: s1ckle
3 Replies

5. Shell Programming and Scripting

pipe result from grep

Trying to create a command line script to look for all files matching a pattern, grep for a specific value in each file, and write out the filename long list. It's possible the filename won't containe the value. { echo “Running....” for fname in 811_Intermediate_File_* do grep -l... (3 Replies)
Discussion started by: gavineq
3 Replies

6. UNIX for Dummies Questions & Answers

Grep alternative to handle large numbers of files

I am looking for a file with 'MCR0000000716214' in it. I tried the following command: grep MCR0000000716214 * The problem is that the folder I am searching in has over 87000 files and I am getting the following: bash: /bin/grep: Arg list too long Is there any command I can use that can... (6 Replies)
Discussion started by: runnerpaul
6 Replies

7. Shell Programming and Scripting

How to negate grep result?

Here is my script so far: set dirs = ` find . -name "message.jar" 2> /dev/null | cut -d "/" -f 2 ` | uniq foreach dir ( $dirs ) if (grep $dir/* someText==null) --> how do I write this in script? print $dir end end (4 Replies)
Discussion started by: mmdawg
4 Replies

8. UNIX for Advanced & Expert Users

How to handle backslash in grep string

Hi , I am doing invert grep using -v but the string contain "/" which break the grep command and it do not skip the lines with "/" on it. Diffu.txt ======== 1159c1159 < <td align="right" valign="middle" class="paddingRight2px" id="featureListItemChannelButton7466"> --- > <td... (6 Replies)
Discussion started by: rajbal
6 Replies

9. Shell Programming and Scripting

How ro handle backslash character in grep?

Hi , I am doing invert grep using -v but the string contain "/" which break the grep command and it do not skip the lines with "/" on it. Diffu.txt ======== 1159c1159 < <td align="right" valign="middle" class="paddingRight2px" id="featureListItemChannelButton7466"> --- > <td... (1 Reply)
Discussion started by: rajbal
1 Replies

10. UNIX for Dummies Questions & Answers

To have a numeric result from grep

I am new to unix. i need to know how to use grep to grep and expression from a file. and pass the result as a 0 for found and 1 for not found. I can only go up to grep 'Checking Subscription Status' ranos.log. Please help. Thank you. (2 Replies)
Discussion started by: Hak Dee
2 Replies
Login or Register to Ask a Question