LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 12-09-2013, 05:46 AM   #1
gargaks83
LQ Newbie
 
Registered: Sep 2013
Posts: 10

Rep: Reputation: Disabled
see only unique strings


Hi Team,

I need to know if there is multiple strings in a file out of which few are same, so is it possible that we can see only unique strings of that file ?

Thanks.
 
Old 12-09-2013, 05:55 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
If those are plain text files you could use diff. Else state what files you wanna compare.
 
Old 12-09-2013, 06:55 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,421

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
Generally helps if you:

1. State the problem

2. Provide example data

3. Show us what you have attempted

4. Explain where you are stuck

Otherwise you will get more bland answers like above as the only real answer here - yes it is possbile
 
Old 12-09-2013, 11:56 PM   #4
gargaks83
LQ Newbie
 
Registered: Sep 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
e.g.:
Below is my file:

cat file1
name = A
name = A
name = A
name = B
name = B
name = C

I want the below output:

name = A
name = B
name = C

(want to grep the unique strings only)
 
Old 12-10-2013, 01:07 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,421

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
Firstly, please use [code][/code] tags for data and code.

Your most recent post covers step 1 and 2 fom my previous post ... what about 3 and 4??
 
Old 12-10-2013, 01:54 AM   #6
gargaks83
LQ Newbie
 
Registered: Sep 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Code:
cat file1
name = A
name = A
name = A
name = B
name = B
name = C
 
Old 12-10-2013, 02:38 AM   #7
gargaks83
LQ Newbie
 
Registered: Sep 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
got the answer:
Code:
cat file1 | uniq
 
1 members found this post helpful.
Old 12-10-2013, 02:39 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,332
Blog Entries: 55

Rep: Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533
Unless there's more to it see 'man sort'.
 
Old 12-10-2013, 03:17 AM   #9
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 516

Rep: Reputation: 139Reputation: 139
..or just
Code:
uniq file1
As unSpawn has mentioned, it is always recommended to sort before applying uniq.

What if,

Code:
$ cat file1
name = A
name = A
name = A
name = B
name = B
name = C
name = A

$ uniq file1
name = A
name = B
name = C
name = A

$ sort file1 | uniq
name = A
name = B
name = C

Last edited by mddnix; 12-10-2013 at 03:22 AM.
 
1 members found this post helpful.
Old 12-10-2013, 08:11 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,421

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
Or even a simple awk and then sorting is not required
Code:
awk '!_[$0]++' file
As you can see, once you have shown some effort and where you might be having issues, all sorts of solutions will be presented
 
1 members found this post helpful.
  


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
Something Unique nankura Linux - Newbie 8 11-24-2011 06:47 AM
BASH: replace strings in on file by the strings in another one cristalp Programming 5 10-28-2011 10:47 AM
[SOLVED] BASH: Write only unique strings to text file (cat or while read question) SilversleevesX Programming 32 08-11-2010 03:24 AM
[SOLVED] Searching and replacing strings in a file with strings in other files xndd Linux - Newbie 16 07-29-2010 03:40 PM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 05:04 AM


All times are GMT -5. The time now is 08:06 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration