Operating System - HP-UX
1826443 Members
3967 Online
109692 Solutions
New Discussion

Exit after first 'found' in grep

 
SOLVED
Go to solution
Aravind_3
Frequent Advisor

Exit after first 'found' in grep

i have a text file
_________________________
syslog file is correct
i am not correct
_________________________

if i do
grep "correct" txt
it will return both the lines,

How do i exit after the first "correct" is grepped ?

Aravind
4 REPLIES 4
Christian Gebhardt
Honored Contributor

Re: Exit after first 'found' in grep

Hi

with awk it's something like this:

awk '/correct/ {print $0;exit}' file

regards
Christian
Christian Gebhardt
Honored Contributor
Solution

Re: Exit after first 'found' in grep

if you like grep try this:
grep correct a | head -1

Christian
Elif Gius
Valued Contributor

Re: Exit after first 'found' in grep

Hi ,

You can also use:
cat a |grep correct | head -1
Rodney Hills
Honored Contributor

Re: Exit after first 'found' in grep

If you just want to test if the search text is in the file, then you can use the "-q" option.

if grep -q "correct" txt ; then
echo "correct is in file txt"
else
echo "no correct found!"
fi

"-q" forces grep to stop after the first instance is found.

HTH

-- Rod Hills
There be dragons...