LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-27-2008, 08:02 AM   #1
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Rep: Reputation: 15
sort & uniq


Hi there,

I have a file as below

K1|K2|dataA1|K3|dataA2|K4|dataA3
K1|K2|dataB1|K3|dataB2|K4|dataB3


what I need to do is sort on the keys K1,K2,K3,K4 and remove any duplicates on them (only one should remain, could be any thing). How can I do that using sort and uniq.
 
Old 06-27-2008, 08:09 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You might like to use the -u option to sort. No need to use uniq (which operates per line, not per sort key).
 
Old 06-27-2008, 08:15 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Use awk or perl and

(1) Read the input and build a HASH array with hash value as k1+k2+k3+k4 and two data values having the count and any one data set (say the first).
(2) On end of file, scan the hash array and dump out the elements where the count > 1.

End
 
Old 06-28-2008, 06:14 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Code:
sort -u -t\| -k1,1 /tmp/file.txt
 
  


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
Quick question about uniq Seventh Linux - Software 1 01-07-2008 10:27 AM
gawk & sort commands in unix fanatic_ravi Linux - Software 0 01-25-2005 04:10 AM
USB Pen drive & camera - Auto sort? r_jensen11 Linux - Hardware 1 07-22-2004 11:07 PM
uniq in tcl/tk liguorir Linux - Software 1 05-20-2004 10:34 PM
uniq Cyth Linux - General 2 02-19-2003 09:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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