Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-29-2012, 12:02 PM   #1
Registered: Jan 2007
Posts: 36

Rep: Reputation: 15
sort and uniq conundrum


Apologies for posting what should be a very simple task but hopefully someone can give me the correct command to sort uniquely from the 1st word in the file. ie.

The file would say for example:

hostname1 17/08/10
hostname1 18/08/10
hostname1 19/08/10
hostname1 20/08/10
hostname2 17/08/10
hostname2 18/08/10
hostname2 19/08/10
hostname2 20/08/10

So after the sort I simply have:

hostname1 17/08/10
hostname2 17/08/10

Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-29-2012, 12:32 PM   #2
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Grep is the tool use. Lets say the data is in a file named dates.txt. To use grep to find the records with the date 17/08/10, the grep command would be:
grep "17/08/10" dates.txt
Old 03-29-2012, 07:43 PM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,928

Rep: Reputation: 2631Reputation: 2631Reputation: 2631Reputation: 2631Reputation: 2631Reputation: 2631Reputation: 2631Reputation: 2631Reputation: 2631Reputation: 2631Reputation: 2631
sort -u -k1,1 <filename>

# output ...
hostname1 17/08/10
hostname2 17/08/10
2 members found this post helpful.
Old 03-30-2012, 03:13 AM   #4
LQ Newbie
Registered: Feb 2012
Posts: 26
Blog Entries: 1

Rep: Reputation: Disabled
i think this would help

sort output.txt | grep 17
Old 03-30-2012, 05:08 AM   #5
Registered: Jan 2007
Posts: 36

Original Poster
Rep: Reputation: 15
Thanks Chris, sort -u -k1,1 <filename> worked...

Also works on a CSV file (with -t) which is the next job...



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
[SOLVED] using sort and uniq in bash bibiki Linux - Newbie 2 02-19-2011 11:12 AM
history |tr '\011' ' ' |tr -s " "| cut -d' ' -f3 |sort |uniq -c |sort -nbr |head -n10 alan_ri General 12 12-04-2010 10:01 PM
[SOLVED] bash - merging strings (perhaps with sort | uniq) cmbouchard Linux - Newbie 4 11-17-2010 12:21 AM
Is there a sort of 2nd SSH daemon server, sort of JAILKIT, in the debian repositories frenchn00b Debian 5 06-20-2010 05:03 AM
sort & uniq tostay2003 Programming 3 06-28-2008 07:14 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:36 AM.

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