find uniq lines in file, using the first field of line
Hello all, new to unix and have just found the forum.
I think I will be here quite often, and hope that in time i will be able to provide soem help, role on not being a newbie anymore
I have a question which iI am hoping someone could help me with.
If i have a file with lines in in thus ...
purple 2 color
purple 3 color
purple 4 color
purple 5 color
blue 1 color
blue 2 color
blue 3 color
how do i sort the list so only uniq instances of field 1 show with the highest value of field 2 ?
See what you mean. I am glad your on the ball, I had only tried it on the example I posted, just tried again with your revised solution (and added numbers in there too) and all is ok.
could I trouble you and ask for a brief breakdown on how it works?
I have ordered the sed & awk book from amazon, hope i understand it when it comes LOL
My understanding was that by using -nrk2,1 the fields would be sorted (reverse) numerically by field 2, then by field 1 (presumably alphabetically - which was wrong).
If you try
Then that's OK (numerically), but the rest (-nrk2,1) doesn't sort the first field after that as I thought.
Sort is a powerful command if you can master it - something I have neither the time nor inclination to do!
Given that the records are sorted reverse numerically in field 2 with "sort -nrk2"...
the awk says:
if I don't have a color (where $1 = purple, or whatever) in my array (C[purple]), then define something (anything) for purple (C[$1]=1) so that I do, and then print the line. If I do have something already defined, then do nothing (thus printing only the first line with each (sorted) color).
Last edited by Scott; 10-20-2009 at 04:40 PM..
Reason: added the word "reverse" in a couple of places, for clarity
thanks, that has helped me make a bit of sense out of what, to me at the moment, seems like voodoo magic
I look forward to the point where I may be able to help someone on these forums, although I had better not hold my breath, i think i have a lot to learn.
once again, many thanks for your kind help, it's appreciated.
I have a file where every line includes four expressions with a caret in the middle (plus some other "words" or fields, always separated by spaces). I would like to extract from this file, all those lines such that each of the four expressions containing a caret appears in at least four different... (9 Replies)
Hi ,
i have a file with data as below.This is same file. But actual file contains to many rows.
i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ?
Field 004... (7 Replies)
Platform :Oracle Linux 6.4
Shell : bash
The below file has 7 lines , some of them are duplicates. There are only 3 distinct lines. But why is the uniq command still showing 7 ?
I just want the distinct lines to be returned.
$ cat test.txt
SELECT FC.COORD_SET_ID FROM OM_ORDER_FLOW F, -... (2 Replies)
Hi All,
I am searching for a script which will produce an output file with the uniq first field with the second field having highest value among all the duplicates..
The output file will produce only the uniqs which are duplicate 3 times..
Input file
X 9
B 5
A 1
Z 9
T 4
C 9
A 4... (13 Replies)
Hi,
I would like to know how to solve one of my problems using expert unix commands.
I have a file with occasional blank lines;
for example;
dertu
frthu
fghtu
frtty
frtgy
frgtui
frgtu
ghrye
frhutp
frjuf
I need to edit the file so that the file looks like this; (10 Replies)
My sql file xyz_abc.sql in this file there are multiple sql block in this block I need to find the following block
rem Subset Rows (&&tempName.*)
CREATE VIEW &&tempName.* AS
SELECT *
FROM &&tempName.*
WHERE f is not null
and replace with following code
rem Subset Rows... (9 Replies)
Hi all,
Sorry for the title, I was unsure how to word my issue. I'll get right to the issue. In my text file, I need to find all lines with the same data in the first field. Then I need to create a file with the matching lines merged into one. So my original file will look something like... (4 Replies)
So, I have a file that has some duplicate lines. The file has a header line that I would like to keep at the top.
I could do this by extracting the header from the file, 'sort -u' the remaining lines, and recombine them. But they are quite big, so if there is a way to do it with a single... (1 Reply)
i have a file a.txt and following is only one portion.
I want to search <branch value="/dev36/AREA/" include="yes"></branch> present in between
<template_file name="Approve External" path="core/approve/bin" and </template_file>
where the no of lines containing "<branch value= " is increasing ... (2 Replies)
Hi ;
I have a question regarding the uniq command in unix
How do I uniq 3rd field in a file ?
original file :
zoom coord 39 18652 39 18652
zoom coord 39 18653 39 18653
zoom coord 39 18818 39 18818
zoom coord 39 18840 39 18840
zoom coord 41 15096 41 15096
zoom... (1 Reply)