grep usage - exit after first occurrence


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep usage - exit after first occurrence
# 1  
Old 10-12-2004
grep usage - exit after first occurrence

Hi,

Is there any way of using grep (this may be done in awk, not sure?) that I can stop grep'n a file once I have found the first occurrence of my search string. Looking through grep man pages
-q will exit without printing the lines after the first match, but I need the output.

I have looked at awk but am not finding what i require.

Any ideas,

Cheers and beers,

Neil
# 2  
Old 10-12-2004
How about something like

Code:
while read line; do
  echo $line | grep "searchstring"
  if [ "$?" -eq "0" ]; then
     break
  fi
done < input_file

Cheers
ZB
# 3  
Old 11-30-2007
Quote:
Originally Posted by zazzybob
How about something like

Code:
while read line; do
  echo $line | grep "searchstring"
  if [ "$?" -eq "0" ]; then
     break
  fi
done < input_file

Cheers
ZB
grep -m 1 <expression> <file_name>


hope it will work.
cheers
# 4  
Old 11-30-2007
-m is fine for GNU grep, but isn't portable across older (or even new vendor supplied) grep implementations.

Cheers,
ZB
# 5  
Old 11-30-2007
sed:

Code:
sed -n '/pattern/{p;q;}' filename

# 6  
Old 12-05-2008
Alternative...

You could pipe the results of your grep through head, it would look something like this:

Code:
grep <patern> <file> | head -1

if your file is incredibley large this may not be what you want, but if all you want to know is the first thing that grep found, and time wont be an issue, then this may be the most simple...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep util last occurrence

I have file contents /tmp/x/abc.txt /home/bin/backup/sys/a.log I need this output: /tmp/x/ /home/bin/backup/sys/ Can somebody please help me out Please use CODE tags as required by forum rules! (3 Replies)
Discussion started by: jhonnyrip
3 Replies

2. Shell Programming and Scripting

Substitute first occurrence of keyword if occurrence between two other keywords

Assume a string that contains one or multiple occurrences of three different keywords (abbreviated as "kw"). I would like to replace kw2 with some other string, say "qux". Specifically, I would like to replace that occurrence of kw2 that is the first one that is preceded by kw1 somewhere in the... (4 Replies)
Discussion started by: M Gruenstaeudl
4 Replies

3. UNIX Desktop Questions & Answers

grep a specific amount of occurrence

hey , i m trying to figure out how to do the following : i got a text file the looks like so: 1031 1031 1031 1031 1031 1031 1031 1031 16500 16500 16500 16500 1031 1031 (4 Replies)
Discussion started by: boaz733
4 Replies

4. Shell Programming and Scripting

grep and exit

I have a scenario, where i need to grep for the information and then exit. i have to grep for "import done" and "bye" from success.log and then update a file with success if these words are present and failure if they are not present. can any one help with this scenario? (4 Replies)
Discussion started by: chinuku
4 Replies

5. UNIX for Dummies Questions & Answers

grep first occurrence but continue to next entry in patternfile

I have 1300 files (SearchFiles0001.txt, SearchFiles0002.txt, etc.) , each with 650,000 lines, tab-delimited data. I have a pattern file, with about 1000 lines with a single word. Each single word is found in the 1300 files once. If I grep -f PatternFile.txt SearchFiles*.txt >OutputFile.txt... (2 Replies)
Discussion started by: newhavendweeb
2 Replies

6. Shell Programming and Scripting

Exit status of grep

I am trying to get the exit status of grep and test a condition with it, But it does not seem to be working as expected since i am doing something wrong apparently as per grep help Exit status is 0 if match, 1 if no match, and 2 if trouble. My problem is something like this templine - a... (7 Replies)
Discussion started by: prasbala
7 Replies

7. UNIX for Dummies Questions & Answers

Line number of an occurrence using grep

Hi All, is there a way to extract the line number of an occurrence using grep? I know that with the -n option it prints out the line number as well. I would like to assign the line number to a variable. Thanks, Sarah (5 Replies)
Discussion started by: f_o_555
5 Replies

8. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

9. Programming

Usage of exit() inside a signal handler

Is it ok to use exit() inside a signal handler? I catch SIGUSR1 in a signal handler and I try to close a file and then exit. The result is inconsistent. Sometimes the process exit and sometimes it returns to the original state before the signal handler was invoked. Perhaps exit is not legal in... (8 Replies)
Discussion started by: Tuvia
8 Replies

10. Shell Programming and Scripting

Grep for the same occurrence or maybe Sed

Hi, I have a file that looks like this dasdjasdjoasjdoasjdoa SYN dakspodkapsdka asdasdasdasdasdasdasd SYN sdfsdfsdfsdfdf shfishifhsdifhsidhfif fsdfsdfsdfsdfs sdfsdfsdfsdsdfsdfsdff cercercercerce sdasdajsdoajsodasodoo FIN dasdaskdpasdda... (4 Replies)
Discussion started by: hcclnoodles
4 Replies
Login or Register to Ask a Question