Your favorite command and mine - grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Your favorite command and mine - grep
# 1  
Old 01-06-2006
Your favorite command and mine - grep

Is their a way to grep for only one occurrence? Here is an example:

given the file information:

BCX 3 5
BCX 23 94
BNG 34 34
BCX 2 09

if I do:

Code:
grep BCX ./file

then i get them all, how can i get just one of them. But let's say there could be a variable number of the desired string. Thanks in advance.
# 2  
Old 01-06-2006
Do you only want to test the presence of BCX in the file ?

You can do the following

Code:
grep -q 'BCX' file && echo "BCX found" || echo "No BCX found"

If you need only one occurence, then

Code:
grep -m 1 'BCX' file

-m and -q are available on GNU grep.
# 3  
Old 01-06-2006
based on which occurence you need,

Code:
grep BCX file | awk 'if NR == <occurence_no_needed> {print }'

Code:
grep BCX file | head -1

Is the above your requirement ?
# 4  
Old 01-06-2006
Code:
$ cat wxornot
BCX 3 5
BCX 23 94
BNG 34 34
BCX 2 09
$ while read line; do
>   echo "${line}" | grep BCX && break
> done < wxornot
BCX 3 5

If you want to assign a value to a variable, then
$ FOO="BCX"
then change the grep to
... | grep "${FOO}" && break

Cheers
ZB
# 5  
Old 01-06-2006
How about:
sed -n '/BCX/ {p;q;}' < data
# 6  
Old 01-07-2006
Quote:
Originally Posted by Perderabo
How about:
sed -n '/BCX/ {p;q;}' < data
Perderabo,

Code:
sed -n '/BCX/ {p;q;}' < data

works very well like
Code:
sed -n '/BCX/ {p;q;}' data

Is there any difference in the above command, other than syntax ?

Vino
 
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. What is on Your Mind?

What are Your Favorite Chips? Mine are Doritos Nacho Cheesier!

By far, Doritos Nacho Cheesier are the best tasting chips, well at least too me. Have you tried them? What are your favorite chips? (5 Replies)
Discussion started by: Neo
5 Replies

2. Fedora

od -c . does not work on mine

# od -c . od: .: read error: Is a directory 0000000 I read the book about shell (Kernighan, Pike) and some exaples (like this) does not work. I read this book carefully (because I think now quickly) even reread to better understand. In this book there is a good example how to see what a... (2 Replies)
Discussion started by: Xcislav
2 Replies

3. What is on Your Mind?

Where Is Your Favorite Continent?

Where is your favorite continent to enjoy life? (14 Replies)
Discussion started by: Neo
14 Replies

4. What is on Your Mind?

What's your favorite Xscreensaver?

Out of the 100s of screensavers in xscreensaver, what is your favorite? I think mine is definitely juggle. I can sit and watch that juggler forever. I haven't looked into the code, but it must be amazingly complex to deal with all that's going on. From the random throwing pattern, to the arm... (0 Replies)
Discussion started by: tjlst15
0 Replies
Login or Register to Ask a Question