How to scan a file for literal, return 0 or 1 if found?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to scan a file for literal, return 0 or 1 if found?
# 8  
Old 07-31-2007
Quote:
Originally Posted by kahuna
I think you meant

Code:
if grep -i error filename 1>/dev/null 2>&1
then
echo "found"
else
echo "not found"
fi

No !

with this

Code:
`grep -i error filename 2>/dev/null 1>&2` -eq 0

the return value of grep command is directly equated to the value 0

o - success pattern has been found
other value - pattern not found
# 9  
Old 07-31-2007
Matrix,
Your solution:
Code:
if [ `grep -i error filename 1>/dev/null 2>&1` -eq 0 ]

does not work.

Kahuna is right in fixing your 'if' statement:
Code:
if grep -i error filename 1>/dev/null 2>&1

It is important to note that the option '-i' does not work as it
greps for more than just 'error' and 'ERROR':
'Error', 'eRROR', 'ErRoR', or any other combination of mixed cases.
# 10  
Old 07-31-2007
Quote:
Originally Posted by matrixmadhan
No !
the return value of grep command is directly equated to the value 0
o - success pattern has been found
other value - pattern not found
The code does not work as written. Please try to run it. The return value from grep will not be used in the if statement. Perhaps you meant to add a "-c"?
# 11  
Old 08-02-2007
I accept ! Smilie

Code:
cat test2
pattern

Code:
grep -i pattern test2
if [ $? -eq 0 ]
then
  echo "found"
else
  echo "not found"
fi

Code:
if grep pattern test2 1>/dev/null 2>&1
then
  echo "found"
else 
  echo "not found"
fi

based on success or failure of the grep command
# 12  
Old 08-02-2007
FWIW:
use grep -q to suppress output:

Code:
grep -q 'some string' myfile ; echo $(( ! $? ))

will show 1 if found and 0 if not found - ksh and bash.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Fgrep literal string from a file

have a file1 aaa-bbb-ccc-abcd aaa-bbb-ccc-bacd aaa-bbb-ccc-aaad aaa-bbb-ccc-ahave another file2 aaa-bbb-ccc-a fileusing the fgrep command, trying to have only the literal string returned. fgrep -f file2 file1 is returning aaa-bbb-ccc-abcd aaa-bbb-ccc-aaad aaa-bbb-ccc-aOnly looking for... (1 Reply)
Discussion started by: jimmyf
1 Replies

2. Shell Programming and Scripting

Return first two columns if match found among two files

Hi, I have FileA with one column. File B with 15 columns separated by comma delimiter. I need to compare the FILEA value with all 15 columns of FILEB... if matches, need to return the 1st, 2nd column values of FILEB. How to achieve this through shell script? Thanks in advance. (5 Replies)
Discussion started by: vamsikrishna928
5 Replies

3. UNIX for Dummies Questions & Answers

How to find a file based on pattern & return the filename if found?

Hi all, I am a newbie here. I have this requirement to find a file based on a pattern then return the filename if found. I created a script based on online tutorials. Though, I am stuck & really appreciate if anyone can have a quick look & point me to the right direction? #Script starts... (10 Replies)
Discussion started by: buster_t
10 Replies

4. Shell Programming and Scripting

Avoid carriage return until ^M is found (CentOS 6, bash 4.1)

Hi everyone, I have the following contents in a text file (as seen when viewed using vim): one two three ^M four five six ^M seven eight nine ^M ten eleven twelve ^M (That is just a small portion of the file) How can I obtain the following result? one two three ^M four five six ^M seven... (2 Replies)
Discussion started by: gacanepa
2 Replies

5. Shell Programming and Scripting

awk Help -- If match found return the count

Hi All, I need to get the count of records in the file, if the passing parameter matches with the list of records in the file. Below is my example source file: Test1.dat 20120913 20120913 20120912 20120912 20120912 20120912 20120912 20120913 20120913 20120912 In my script I am... (5 Replies)
Discussion started by: bbc17484
5 Replies

6. Shell Programming and Scripting

bash: need to have egrep to return a text string if the search pattern has NOT been found

Hello all, after spending hours of searching the web I decided to create an account here. This is my first post and I hope one of the experts can help. I need to resolve a grep / sed / xargs / awk problem. My input file is just like this: ----------------------------------... (6 Replies)
Discussion started by: bash4ever
6 Replies

7. Shell Programming and Scripting

Bash: Reading out rows of a file into a dynamic array and check first literal

Hello, i have a file "Movie.ini" looking e.g. like follows * MOVIE A bla bla MOVIE B blubb blubb MOVIE C I'd like to read the file "Movie.ini" with cat and grep and check whether it includes the string MOVIE only with a '*' at the beginnig. By doing "cat Movie.ini| grep MOVIE... (14 Replies)
Discussion started by: ABE2202
14 Replies

8. Shell Programming and Scripting

Scripting problem - when the file is not found i want it to return to the menu

when the file is not found i want it to return to the menu, however it carries out the next line when i hit a key I know its probably something simple can anyone help? here is my pause function: function pause(){ read -s -n 1 -p "Press any key to return to Menu . . ." echo } SCRIPT... (2 Replies)
Discussion started by: Alendrin
2 Replies

9. Shell Programming and Scripting

Return a message when a file is not found

Hi there, I am writing a script to look for tmp log files that have not been access within the last 10 days. I am using the follwing command within the script: find /var/tmp -name *log -atime -9 ¦xargs What I would like to be able to do would be to display a message if there is no... (3 Replies)
Discussion started by: lodey
3 Replies

10. Shell Programming and Scripting

File Scan

Hi everyone , i m working on Sun solaris and i have a file "smsapp.cur" which has information like this paragraph given below , there are millions of such paragraphs From:923212802736 To:923222326807 logMessage: 07-04-08 17:34:29 Getting message topup from code page default in language English... (2 Replies)
Discussion started by: Dastard
2 Replies
Login or Register to Ask a Question