grep for word not working


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep for word not working
# 1  
Old 09-26-2011
Java grep for word not working

Hi All..I need a help i am trying to find a word using below script whereas the word exists in my file nitin.txt as a directory but still i am getting "word not found" output..Your suggestions welcomed.:
#to check for existence of nitin
Code:
#!/bin/bash
cd /apps/uat1/deploy/app

ls -lrt > nitin.txt


FILE=/apps/uat1/deploy/app/nitin.txt

grep -w "*nitin*" $FILE >/dev/null

if [ $? -eq 0 ]
then
   echo "Word found!"
else
   echo "Word NOT found!"
fi


Last edited by Scott; 09-26-2011 at 06:48 AM.. Reason: Code tags
# 2  
Old 09-26-2011
Hi, you are using wild cards (*) as in a globbing situation, whereas grep uses regular expressions so the equivalent would be ".*nitin.*". With grep this makes little sense however.

If you are looking for the word nitin, you could use this:
Code:
grep -w nitin "$FILE"

or for words that contain nitin:
Code:
grep nitin "$FILE"

additionally you can use -q to silence grep, so you could do something like this:
Code:
if grep -q nitin "$FILE"
then
   echo 'Word found!'
else
   echo 'Word NOT found!'
fi


Last edited by Scrutinizer; 09-26-2011 at 06:11 AM..
# 3  
Old 09-26-2011
Thnaks Scrutinizer!!!

It is helpful..
can you please tell me the case when i have to search nitin and there are entry in file like delhiNitin_Unix then how to match nitin here in same situation as explained above just .

what we need to change in below pattern:
grep -w nitin "$FILE"
# 4  
Old 09-26-2011
I gather you do not require a word match, so we do not need -w and we need -i to ignore upper and lower case distinction, so I'd say:
Code:
grep -i nitin "$FILE"

# 5  
Old 09-26-2011
Thnx a lot Scrutinizer Smilie..its working ..Now i am moving forward in my script ..
I will keep following you for next times also.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep for a word or word with underscore

I have a file "test" with following contents: cat test abc abcd_efg abc_abc I want to only grep for abc or abc_ without getting other results, how do I achieve this? If I use grep -w abc test option I get only abc and not abc_. If I use egrep "abc|abc_" test its still printing... (3 Replies)
Discussion started by: ctrld
3 Replies

2. Shell Programming and Scripting

awk word boundaries not working

Hi, I am trying below code but the word boundaries not seem to be working. What am I doing incorrectly? echo " ECHO " | awk '{ q="ECHO" ; if ( $0 ~ /\bq\b/) print "HELLO" ; }' OR echo " ECHO " | awk '{ q="ECHO" ; if ( $0 ~ /\b'$q'\b/) print "HELLO" ; }' Or echo " ECHO " | awk... (6 Replies)
Discussion started by: ahmedwaseem2000
6 Replies

3. Shell Programming and Scripting

awk - why this is not working? trying next word!

Hi Experts, Can you please advise , why I am not able to make it work, or why this is not working: I spent quite a lot of time on this figuring out , but not working, file : This is a test file thanks for your reply This is another file again Have a nice day this is a small file... (3 Replies)
Discussion started by: rveri
3 Replies

4. Shell Programming and Scripting

Need a word which just comes next to after grep of a specific word

Hi, Below is an example : ST1 PREF: int1 AVAIL: int2 ST2 PREF :int1 AVAIL: int2 I need int1 to come in preferred variable while programming and int2 in available variable Please help me doing so Best regards, Vishal (10 Replies)
Discussion started by: Vishal_dba
10 Replies

5. Shell Programming and Scripting

How ti Grep for a word and print the next word

Hi can we grep for a word and print the next word of the greped word? ex:- create or replace function function_name create function function_name we should search for word "function" and output next word "function_name" from both lines. (3 Replies)
Discussion started by: manasa_vs
3 Replies

6. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

7. Shell Programming and Scripting

How to grep a word and next column to that word?

Hi, I have input file as below. Can you help me? inac_4y;0;2;Balance;200;1;1; 0;2;Balance;100;1; 0;inac_nq;0;1;Balance;100;1 desired output Balance;200 Balance;100 Balance;100 -Suresh Please use and tags when posting code, data or logs etc. to preserve formatting... (5 Replies)
Discussion started by: suresh3566
5 Replies

8. Shell Programming and Scripting

Grep out specific word and only that word

ok, so this is proving to be kind of difficult even though it should not be. say for instance I want to grep out ONLY the word fkafal from the below output, how do I do it? echo ajfjf fjfjf iafjga fkafal foeref afoafahfia | grep -w "fkafal" If i run the above command, i get back all the... (4 Replies)
Discussion started by: SkySmart
4 Replies

9. UNIX for Dummies Questions & Answers

how to grep the word and display only the second word from it

hi, consider the below line in a text file, 'Y',getdate(),'N','V',NULL ..... 'N',getdate(),'Y','D',NULL ..... 'Y','N','Y',getdate(),'Y','D',NULL .... as u see above, i want only the second word after the getdate() word... getdate() will not come 2nd word alwys it may be any position but i... (11 Replies)
Discussion started by: prsam
11 Replies

10. UNIX for Dummies Questions & Answers

how to grep for a word and display only the word

Hi, When we "grep" for a word in a file, it returns the lines containing the word that we searched for. Is there a way to display only the words and not the entire line containing them. Thanks Ananth (6 Replies)
Discussion started by: ananthmm
6 Replies
Login or Register to Ask a Question