LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-24-2017, 08:54 AM   #1
gaetan321
LQ Newbie
 
Registered: Nov 2017
Posts: 5

Rep: Reputation: Disabled
remove duplicate results from a grep


grep returns

notification_interval 60
notification_interval 60
notification_interval 60
notification_interval 30
notification_interval 120
notification_interval 120

How would I remove the duplicate results ?

notification_interval 60
notification_interval 30
notification_interval 120

I tried using a combination of options with uniq but it still returns duplicates
grep notification_interval /usr/local/centreon/filesGeneration/nagiosCFG/1/serviceTemplates.cfg | uniq -d

notification_interval 60
notification_interval 60
notification_interval 120
notification_interval 60
notification_interval 180
notification_interval 180
notification_interval 90
notification_interval 30
notification_interval 60
notification_interval 30

Last edited by gaetan321; 11-24-2017 at 08:56 AM.
 
Old 11-24-2017, 09:01 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,412

Rep: Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540
Pipe through sort before piping in to uniq
 
1 members found this post helpful.
Old 11-24-2017, 09:03 AM   #3
gaetan321
LQ Newbie
 
Registered: Nov 2017
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by TenTenths View Post
Pipe through sort before piping in to uniq
That makes sense. Thks
 
Old 11-24-2017, 09:16 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,307

Rep: Reputation: 1021Reputation: 1021Reputation: 1021Reputation: 1021Reputation: 1021Reputation: 1021Reputation: 1021Reputation: 1021
awk can remove duplicates without sorting
Code:
awk '/notification_interval/ && s[$0]++==0' /usr/local/centreon/filesGeneration/nagiosCFG/1/serviceTemplates.cfg
In this case you can even minimize resources by utilizing awk's word splitting
Code:
awk '$1=="notification_interval" && s[$2]++==0' /usr/local/centreon/filesGeneration/nagiosCFG/1/serviceTemplates.cfg
 
1 members found this post helpful.
Old 11-24-2017, 09:32 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 19,880

Rep: Reputation: 6735Reputation: 6735Reputation: 6735Reputation: 6735Reputation: 6735Reputation: 6735Reputation: 6735Reputation: 6735Reputation: 6735Reputation: 6735Reputation: 6735
yes, awk is a good solution (even grep is not required), but if you want to use sort, use sort -u and do not need to use uniq.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does NIS produce duplicate results when I query from a client? jimieee Linux - Server 3 06-10-2013 08:15 AM
Filter Your Grep Results carlosinfl Linux - General 2 11-01-2010 07:31 PM
customizing grep results graziano1968 Linux - General 6 03-09-2009 07:24 AM
cp files from grep results Sigkill(9) Linux - Newbie 2 08-22-2005 03:17 PM
Using the results of locate for grep skibud2 Linux - Newbie 1 12-16-2003 01:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration