LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-19-2013, 04:57 AM   #1
mitter1989
Member
 
Registered: Sep 2013
Posts: 47

Rep: Reputation: Disabled
Question Is there a way to unique by column/field?


Hello folk,

Is there a command in CentOS to unique by column/field?

My file looks like below :
[root@mitter log]# cat test,txt
test test2
test3 test4
test2 test1
test2 test4


and desired output :

test test2
test2 test1
test3 test4
 
Old 10-19-2013, 05:26 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
When looking at your example: Nope.

There's no way to know which of these 2 need to be removed:
Code:
test2 test1
test2 test4
You can get rid of the first one or the last one, but both are unique.

Here's a way to only show the first occurrence:
Code:
awk '!($1 in array) { array[$1]; print}' <( sort test.txt )
This only shows the last one:
Code:
awk '!($1 in array) { array[$1]; print}' <( sort -r data.file ) | sort
 
Old 10-19-2013, 05:59 AM   #3
mitter1989
Member
 
Registered: Sep 2013
Posts: 47

Original Poster
Rep: Reputation: Disabled
Hello Druuna,

Thanks!

ref : http://stackoverflow.com/questions/1...915773#1915773


sort -u -t, -k1,1 file
 
  


Reply



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
django: get all unique values from model field stateless Programming 0 04-19-2013 02:44 AM
How to display 2 different column field values as one column value in mysql VijayaRaghavanLakshman Linux - General 2 04-16-2012 09:56 AM
[SOLVED] Multiple input into seq command from pipe / single column unique numbers geehog Programming 4 05-14-2011 12:18 AM
error unknown column 'xxx' in field list rajesh84210 Linux - Newbie 4 09-07-2009 11:22 PM
selecting particular field/column in file raghu123 Programming 3 08-13-2008 01:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:27 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