1837240 Members
5494 Online
110115 Solutions
New Discussion

Re: shell script

 
Jacques Carriere
Regular Advisor

shell script

What's the command to sort and keep only lines that have a unique field. Delete all other lines.

eg. sort -k 1,1 $file ....

Jacques
6 REPLIES 6
Christian Tremblay
Trusted Contributor

Re: shell script

sort -k 1,1 $file | grep
James R. Ferguson
Acclaimed Contributor

Re: shell script

Hi Jacques:

The manpages will offer insight. Use the -u' switch.

Regards!

...JRF...
Jacques Carriere
Regular Advisor

Re: shell script

Can't grep. For example, I need to extract line 3 and 4 with identical field 1. The -u option looks at the whole record, not a specific field.
Sandman!
Honored Contributor

Re: shell script

# sort -k1,1 file | uniq -u

...that is you want to keep only those lines whose first field is not repeated?

~cheers
Jacques Carriere
Regular Advisor

Re: shell script

I need the oposite of uniq -u
Hein van den Heuvel
Honored Contributor

Re: shell script

Jacques, please try to describe a little better, perhaps with a 10 line example.

> I need the oposite of uniq -u

That operates on line (skippng some fields), but if I understand you correctly, you want the uniqueness to eb determined by 1 fields, not the whole line.

> What's the command to sort and keep only lines that have a unique field

given a file:
a
b
b
c
b
c
c
d

do you want
1)
a
b
c
d

2)
a
d

3)
b
c

4) (uniq -u)
a
c
b
d

Cheers,
Hein.