Is grep being used correctly?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is grep being used correctly?
# 1  
Old 04-17-2008
Question Is grep being used correctly?

My goal is to find files contain the "signal 11" string in a specific directory. I need the file details followed by the string.

I wrote a script to test out and play with Shell command since it's my first time to write a Shell script.

Let me cut the story short...

this command line: if [ 'grep -i "signal 11" $fil' -ne "" ]
returns nothing, but actually in reality, I have couple files contains "signal 11" string.

My question is, why does the script not return a list of files which contains "signal 11"?
Is there anything wrong with my script that you can see?

Thanks in advance!

==========================================

#!/bin/sh
#
RESULT=/disk2/app/applmgr/appltop/cssm_custom/11.5.0/out/W2.out
if [ -f /disk2/app/applmgr/appltop/cssm_custom/11.5.0/out/W2.out ]
then
rm $RESULT
fi
#List all modified files more then 7 days in the Directory
for fil in `find /disk2/app/applmgr/comntop/admin/log/ARDEV_arebdd -type f -mtime +7`
do
#Check if the file contains "signal 11"
#If Yes, print out the file name to the output file.
if [ 'grep -i "signal 11" $fil' -ne "" ]
then
echo "fil = $fil" >> $RESULT
fi
done
===============================================
# 2  
Old 04-17-2008
Code:
if grep -ic "signal 11" $fil
then
   echo "fil = $fil" >> $RESULT
fi

# 3  
Old 04-18-2008
if your grep supports -l (no need to use if/else )
Code:
grep -l  pattern $fil  >>  out

another way
Code:
awk '/pattern/{print FILENAME;exit}' $file >> out

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep Regexp not working correctly

Consider the following code: grep -o -e '^STEAM_::\d+$' workfile3.tmp A sample format of a valid string for the regexp would be: STEAM_0:1:12345678 Here is an example line from the workfile3.tmp file: 465:L 01/02/2012 - 00:05:33: "Spartan1-1-7<8><STEAM_0:1:47539638><>" connected No... (2 Replies)
Discussion started by: spinner0205
2 Replies

2. Shell Programming and Scripting

Result of the grep is not storred correctly into the variable

I am facing a problem while storring the grep results into a variable. I need to count the occurence of the pattern \, in a file and store that in a variable. I have given the below command p=`grep -c '\\,' filename` But while echoing the variable, i am getting the total number of lines in... (2 Replies)
Discussion started by: renjithv
2 Replies

3. Shell Programming and Scripting

Escaping ** correctly

Hello This should be easy, but bash is giving me headaches. At the command line the following command works: duplicity --include /home --exclude '**' / file:///foo Doing that from a script is not straightforward. Note that it is basically a requirement that I place the... (3 Replies)
Discussion started by: brsett
3 Replies

4. UNIX for Dummies Questions & Answers

How do I grep a Date correctly

I am still a novice at this stuff, but I have searched everywhere and I cant seem to get this working. I am using a database program that I need to pull information from. The command I am using is the following. search /project | grep "date -v -1m "+%Y-%m"" This returns no results, however... (1 Reply)
Discussion started by: trezero
1 Replies

5. Shell Programming and Scripting

Grep/awk not getting the message correctly

I have a script which will take two file as the inputs and take the Value in file1 and search in file2 and give the output in Outputfile. #!/bin/sh #. ${HOME}/crossworlds/bin/CWSharedEnv.sh FILE1=$1 FILE2=$2 for Var in $(cat $FILE1);do echo $Var grep -i "$Var" $FILE2 done > Outputfile I... (2 Replies)
Discussion started by: SwapnaNaidu
2 Replies

6. UNIX for Dummies Questions & Answers

grep -A switch not working correctly with -m

egrep -A 7 -m 2 -h 'Date:|Time:' *.html this is showing only 2 line after the context of the 2nd found match. Is this a bug in grep? egrep -A 7 -m 2 -h 'Time:' *.html - this works correctly (2 Replies)
Discussion started by: zer0
2 Replies

7. Shell Programming and Scripting

if not working correctly

Anyone have an idea why this if statement does not work correctly? "test2.sh" 18 lines, 386 characters #!/usr/bin/sh WARNING=80 CRITICAL=95 check_it() { if ] || ];then echo "YES ] || ]" else echo "NO ] || ]" fi } check_it 80.1 check_it 81.1 (3 Replies)
Discussion started by: 2dumb
3 Replies

8. UNIX for Dummies Questions & Answers

echo not echoing correctly

Here is the file named tuwork.......... 209 200 WZ 6529 SKTNCA01X4X C POI LODI LODI 738 SKTNCA0127T LOD Here is the scipt....... cat tuwork | while read rva do num=`echo $rva | cut -d" " -f1-2` reg=`echo $rva | cut -c10` ocn=`echo $rva | cut -c12-15` x=`echo $rva | cut -c29`... (3 Replies)
Discussion started by: shorty
3 Replies

9. HP-UX

HP-UX will not boot correctly

i've same failure too, but this command boot pri isl not work/not found Thanks! (1 Reply)
Discussion started by: pantas manik
1 Replies

10. UNIX for Dummies Questions & Answers

HP-UX will not boot correctly

I am an intern at a company with an HP visualize C160 running HP-UX B.10.20. It has stopped booting to the gui and, after some coaxing, will end up at a text-based menu screen but virtually nothing can be run from there. At first the computer ran through it's boot process then stopped at this... (5 Replies)
Discussion started by: intern
5 Replies
Login or Register to Ask a Question