Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
am trying to print only unique records by comparing the values only from 1st column.
But its not working as expected.
May i know the required changes to do ???
[root@localhost ~]# uname -a
Linux localhost.localdomain 2.6.32-71.el6.x86_64 #1 SMP Wed Sep 1 01:33:01 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# sort -u -k1 ee
Ankit m 28 developer 7000 Jun 11-06-2016
bavana f 30 tester 10000 Dec 02-12-2016
bavana f 30 tester 900 Dec 02-12-2016
nilakshi f 25 engineer 20000 Feb 23-02-16
praveen m 22 analyst 30000 May 15-05-16
sakshi f 36 manager 60000 Mar 22-03-16
sakshi f 42 manager 60000 Sep 05-09-2016
===================================================
Am expecting the output like below:
[root@localhost ~]# sort -u -k1 ee
Ankit m 28 developer 7000 Jun 11-06-2016
nilakshi f 25 engineer 20000 Feb 23-02-16
praveen m 22 analyst 30000 May 15-05-16
============================================
awk? print only if key field(s) *seen only/exactly ONCE*
Try -k1,1 http://stackoverflow.com/questions/1...n-sort-command
But that doesn't *eliminate* if dups; it just leaves one. (My mistake, sorry)
So, it looks like a more complex solution idea is needed. uniq -u (but on 1 field!)
(maybe |awk instead of -u; 'remember' previous and skip if key-match)
I tried awk'ing for hourS: no success How-to iff key field(s) seen *once*?
It's like a 'inventory' algorithm:
if you have >1, don't list/order; if 1, list=order (0/none N/A)
Anyone want to help? Thanks! Happy New Year
UPDATE: I was gifted this (to solve my 'seen-once' failure):
Code:
#!/usr/bin/awk -f
{
if( $1 in keep)
del[$1]
keep[$1] = $0
}
END{
asorti(keep, nkeep)
for(i in nkeep)
if(!(nkeep[i] in del))
print keep[nkeep[i]]
}
My data is not getting sorted when i want to sort 4th column and then based on 4th column values, 5th column should be sorted.
From the below output, 4th column is sorted properly, but 5th column based on 4th column is not sorted.
[root@localhost ~]# sort -k4,4 -k5,5 emp
praveen m 22 analyst 30000 May 15-05-16
saba f 23 analyst 40000 Jan 22-01-16
Nilan m 36 assistant 800 Apr 05-04-16
ghosh m 29 consultant 80000 Oct 08-10-16
Ankit m 28 developer 7000 Jun 11-06-16
rasheed m 32 developer 9000 Jul 16-07-16
nilakshi f 25 engineer 20000 Feb 23-02-16
Tina f 30 hr 8000 Aug 30-08-16
sakshi f 42 manager 60000 Sep 05-09-16 bavana f 30 tester 10000 Dec 02-12-16
bavana f 30 tester 900 Dec 02-12-16 shreya f 33 vendor 5000 Nov 06-11-09
When i tried with numeric sort in 5th column based on 4th column, it is sorting only based on 5th column.
[root@localhost ~]# sort -k4,4 -nk5,5 emp
Nilan m 36 assistant 800 Apr 05-04-16
bavana f 30 tester 900 Dec 02-12-16
shreya f 33 vendor 5000 Nov 06-11-09
Ankit m 28 developer 7000 Jun 11-06-16
Tina f 30 hr 8000 Aug 30-08-16
rasheed m 32 developer 9000 Jul 16-07-16
bavana f 30 tester 10000 Dec 02-12-16
nilakshi f 25 engineer 20000 Feb 23-02-16
praveen m 22 analyst 30000 May 15-05-16
saba f 23 analyst 40000 Jan 22-01-16
sakshi f 36 manager 60000 Mar 22-03-16
ghosh m 29 consultant 80000 Oct 08-10-16
Expected output:
praveen m 22 analyst 30000 May 15-05-16
saba f 23 analyst 40000 Jan 22-01-16
Nilan m 36 assistant 800 Apr 05-04-16
ghosh m 29 consultant 80000 Oct 08-10-16
Ankit m 28 developer 7000 Jun 11-06-16
rasheed m 32 developer 9000 Jul 16-07-16
nilakshi f 25 engineer 20000 Feb 23-02-16
Tina f 30 hr 8000 Aug 30-08-16
sakshi f 42 manager 60000 Sep 05-09-16
bavana f 30 tester 900 Dec 02-12-16
bavana f 30 tester 10000 Dec 02-12-16
shreya f 33 vendor 5000 Nov 06-11-09
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.