Uniq help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Uniq help
# 1  
Old 02-27-2012
Uniq help

hello

Quote:
input
1 8
1 8
1 3
3 5
2 5
Quote:
output
3 5
2 5
I want to check on first column duplicates and print the unique first and second columns

Quote:
my trial
awk '{print $1}' input | uniq -u
Quote:
trial output
3
2
My trial output is not generating what I needed, i.e the second column.

thanks in advance
# 2  
Old 02-27-2012
Try:
Code:
awk '{a[$1]++;b[$1]=$0}END{for (i in a) if (a[i]==1) print b[i]}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 02-27-2012
uniq does not work that way. Since you're using awk anyway, you can do it entirely there:

Code:
$ awk '{ U[$1]++; L[NR]=$0 }
END {
        for(N=1; N<=NR; N++)
        {
                $0=L[N];
                if(U[$1] > 1) continue; # Skip non-unique column 1
                print;
        }
}' data

3 5
2 5

$

[edit] Jinx! Smilie
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 02-27-2012
Building on the O/P original line:
Code:
awk '{print $1}' input | sort | uniq -u | while read key
do
        grep \^"${key} " input
done

./scriptname
2 5
3 5

Note: This would be grossly inefficient on large files. Had to introduce a "sort" because with hindsight it is a freak the the "uniq -u" works because "uniq" requires sorted input.
This affected the output order.

Last edited by methyl; 02-27-2012 at 01:31 PM.. Reason: add a sort
# 5  
Old 02-27-2012
Quote:
Originally Posted by methyl
Building on the O/P original line:
Code:
awk '{print $1}' input | sort | uniq -u | while read key
do
        grep \^"${key} " input
done

./scriptname
2 5
3 5

Note: This would be grossly inefficient on large files. Had to introduce a "sort" because with hindsight it is a freak the the "uniq -u" works because "uniq" requires sorted input.
This affected the output order.
Its not running just plain page is coming
# 6  
Old 02-27-2012
@bhargavpbk88
Possibly the field separator is not a space character?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Uniq tag

Hello, I have recently imported all my scripts into OS X. There is one unix command that is problematic in the new environment: sort temp7.txt | uniq -u -w4 > temp8.txt The system doesn't seem to like the -w tag. Is there an alternative command? Here is the objective: Data: 1234 aaa... (1 Reply)
Discussion started by: palex
1 Replies

2. Shell Programming and Scripting

Uniq not doing what I want it to

I have a master list of servers. I also have a list of servers I'm not supposed to touch. I'm trying to filter out the list servers that I'm not supposed to touch from the master list of servers, so I will have a "master list of servers I can touch". When I try to filter these I'm not getting... (4 Replies)
Discussion started by: MaindotC
4 Replies

3. Shell Programming and Scripting

uniq commmand

Hi I'm having a file (extract.txt) which contains lots of repeated values i want to extract it only with unique values,while am using the the uniq command It result's with 1510 lines this too have the duplicates,pls help me on this. I attached my extract.txt file as attachment here. ... (2 Replies)
Discussion started by: thelakbe
2 Replies

4. Shell Programming and Scripting

uniq -c

When I do uniq -c on a list of sorted numbers, for eg: 1 1 2 2 2 3 3 4 It outputs 2 1 3 2 2 3 1 4. Now, is there a way to sort on the column that "uniq -c" produced? (2 Replies)
Discussion started by: prasanna1157
2 Replies

5. Shell Programming and Scripting

Keep the last uniq record only

Hi folks, Below is the content of a file 'tmp.dat', and I want to keep the uniq record (key by first column). However, the uniq record should be the last record. 302293022|2|744124889|744124889 302293022|3|744124889|744124889 302293022|4|744124889|744124889 302293022|5|744124889|744124889... (4 Replies)
Discussion started by: ChicagoBlues
4 Replies

6. Shell Programming and Scripting

Help with uniq command

I call.... cat 1.txt | uniq -c Sample of whats in 1.txt vmstat cd exit w cd cd cd newgrp xinit f cd cd cd rlogin (2 Replies)
Discussion started by: Bandit390
2 Replies

7. UNIX for Dummies Questions & Answers

Difference between plain "uniq" and "uniq -u"

Dear all, It's not entirely clear to me from manpage the difference between them. Why we still need "-u" flag? - monkfan (3 Replies)
Discussion started by: monkfan
3 Replies

8. Shell Programming and Scripting

How to use uniq on a certain field?

How can I use uniq on a certain field or what else could I use? If I want to use uniq on the second field and the output would remove one of the lines with a 5. bob 5 hand jane 3 leg jon 4 head chris 5 lungs (1 Reply)
Discussion started by: Bandit390
1 Replies

9. Shell Programming and Scripting

who - uniq output

Hi, I'd like to have a script what takes the 'who' output and grabs the user names and outputs just the user name, and no duplicates. I know I could do something like: who | awk '{print $1}' | uniq -u but I'd like to stay away from using the 'uniq' comand and just use awk. Thanks (5 Replies)
Discussion started by: lochraven
5 Replies

10. HP-UX

help on UniQ

All, Can anybody provide me the links to the documentation on UniQPrint? I need to prepare some documents to help my co-workers to learn UniQPrint. Regards, Vishal (0 Replies)
Discussion started by: vishal_ranjan
0 Replies
Login or Register to Ask a Question