grep unique occurrences


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep unique occurrences
# 1  
Old 05-17-2007
grep unique occurrences

Hi,

How do i grep unique occurrences from a file.

I have a log file with multiple occurrences of 'foo' for instance. When i do:
grep foo, I get all the lines that contain foo.
Is there any way to get only one line for foo?

Example file:

Code:
foo at 09.01am Fri 11 May 2007
foo at 09.13am Fri 11 May 2007
foo at 09.17am Fri 11 May 2007
boo at 09.00am Fri 11 May 2007

when I do grep foo I get 3 lines. I only need one line (it doesn't matter which one). Is this possible?

Many thanks for your help.
# 2  
Old 05-17-2007
Do you have GNU grep ?

Code:
grep -m 1 needle haystack

Or

Code:
sed -n -e "/foo/p;q" file

# 3  
Old 05-17-2007
Code:
sed -n "/foo/{p;q;}" file

Code:
awk ' /foo/ { print ; exit } ' file

# 4  
Old 05-17-2007
Code:
grep whatever file | head -1

# 5  
Old 05-17-2007
Thanks that does the trick.

Also taking it a bit further is there a way to clean up a file so it only has unique occurrences? Say I don't know the patterns to search for, I just want to remove all duplicates. Is this possible?

Thanks
# 6  
Old 05-17-2007
Quote:
Originally Posted by mz043
Thanks that does the trick.

Also taking it a bit further is there a way to clean up a file so it only has unique occurrences? Say I don't know the patterns to search for, I just want to remove all duplicates. Is this possible?

Thanks
Code:
awk ' { arr[$1]=$0 } END { for ( key in arr ) { print arr[key] } } ' filename

# 7  
Old 05-17-2007
Quote:
Also taking it a bit further is there a way to clean up a file so it only has unique occurrences? Say I don't know the patterns to search for, I just want to remove all duplicates. Is this possible?
Code:
sort input_file | uniq

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to strictly grep (with before and after args) for last N number of occurrences?

Here is my grep string to print only the last occurrence, with before and after lines. Note that the tail Argument is sum of B and A args + 1, so that it prints the data of only the last 1 match. Now I need to print last 2 such matches. I thought of doubling the tail arg like 5+5+1 (For -- line),... (2 Replies)
Discussion started by: samjna
2 Replies

2. UNIX for Beginners Questions & Answers

Grep or awk a unique and specific word across many fields

Hi there, I have data with similar structure as this: CHR START-SNP END-SNP REF ALT PATIENT1 PATIENT2 PATIENT3 PATIENT4 chr1 69511 69511 A G homo hetero homo hetero chr2 69513 69513 T C . hetero homo hetero chr3 69814 69814 G C . . homo homo chr4 69815 69815 C A hetero . . hetero is... (10 Replies)
Discussion started by: daashti
10 Replies

3. UNIX for Dummies Questions & Answers

Grep unique 1st column

Hello, I'm trying to used awk but am new to this. I have a file like this: Bob is a good boy Bob is a strange person Bob is a good dancer Jane can party Jane is a good girl Jane is batty I'd like to get this: Bob is a good boy is a strange person is a good dancer Jane... (4 Replies)
Discussion started by: Billyjo
4 Replies

4. Shell Programming and Scripting

GREP between last occurrences of two strings

Hi I have the server.log file as: Server Stopped ABC DEF GHI JKL Server Started MNO PQR STU Server Stopped VWX YZ ABC Server Started Server Stopped 123 456 789 (9 Replies)
Discussion started by: ankur328
9 Replies

5. Shell Programming and Scripting

How to grep on unique id which has request and response on different lines?

Hi I want to find out those unique uids from the log file which have request and response. The log file format is as follows. This log has other irrelevant lines too but each uid should have request and reponse, I need those uids only 2013-04-03 10:51:01,808 INFO <?xml version="1.0"... (4 Replies)
Discussion started by: random_thoughts
4 Replies

6. Shell Programming and Scripting

grep with date & unique output

alert.log has the entries with ORA-XXXX, .... Mon Sep 24 15:08:09 2012 WARNING: inbound connection timed out (ORA-3136) Mon Sep 24 15:08:09 2012 WARNING: inbound connection timed out (ORA-3136) Mon Sep 24 15:08:09 2012 WARNING: inbound connection timed out (ORA-3136) Mon Sep 24 15:15:01... (4 Replies)
Discussion started by: Daniel Gate
4 Replies

7. Homework & Coursework Questions

Du without directory and Grep for occurrences of a word

Assistance on work Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Files stored in ... (1 Reply)
Discussion started by: alindner
1 Replies

8. UNIX for Dummies Questions & Answers

Grep Unique

Hello, I have a file with a list of car makes and specific information for each make. An example is: @Audi:Warranty @Audi:Pricing @Audi:Colors @Acura:Warranty @Acura:Pricing @Acura:Colors and so on through a bunch of makes. I need to make a list in a selection box of just one name of... (4 Replies)
Discussion started by: macbb1117
4 Replies

9. Shell Programming and Scripting

grep for unique value

If i want to grep for a value in a file but display only unique value then which option can i use. ex: Values in the file IP <1.2.3.4> value <2> IP <1.2.3.4> value <2> IP <1.2.3.4> value <3> IP <1.2.3.5> value <1> i should get only the unique value (3 Replies)
Discussion started by: vls1210
3 Replies

10. UNIX for Dummies Questions & Answers

Getting unique list of numbers using grep

Hi, I am going to fetch a list of numbers that starts with "0032" from a file with a format like the given below: " 0032459999 0032458888 0032457777 0032451111 0032452222 0032453333 0032459999 0032458888 0032457777 0032451111 0032452222 0032453333 " I want to get a unique... (6 Replies)
Discussion started by: tinku
6 Replies
Login or Register to Ask a Question