Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete repeated rows from a file

Status
Not open for further replies.

tonivm

Technical User
Mar 2, 2006
64
ES
Hi everybody:
Could anybody tell me how I can delete repeated rows from a file?, this is, for exemple I have a file like this:

0.490 958.73 281.85 6.67985 0.002481
0.490 954.833 283.991 8.73019 0.002471
0.590 950.504 286.241 6.61451 0.002461
0.690 939.323 286.112 6.16451 0.00246
0.790 928.17 285.71 5.87057 0.002451
0.890 917.196 285.503 5.6777 0.002441
0.990 906.277 284.498 5.46275 0.00244
1.090 895.529 283.818 5.43785 0.002431
1.190 884.757 283.098 5.36579 0.002421
1.290 874.22 282.2 5.33933 0.00242
1.390 863.667 281.35 5.01376 0.002411
1.490 853.3 280.55 4.61738 0.00241
1.590 842.962 279.95 4.27487 0.002401
1.690 832.775 279.362 3.77744 0.002391
1.790 822.634 278.532 3.78002 0.00239
1.890 812.608 277.625 3.98339 0.002381
1.990 802.735 276.995 4.17061 0.00238
2.090 792.845 276.65 4.77151 0.002389
..
..

in this case I only would like this:

0.490 958.73 281.85 6.67985 0.002481
0.590 950.504 286.241 6.61451 0.002461
0.690 939.323 286.112 6.16451 0.00246
0.790 928.17 285.71 5.87057 0.002451
0.890 917.196 285.503 5.6777 0.002441
0.990 906.277 284.498 5.46275 0.00244
1.090 895.529 283.818 5.43785 0.002431
1.190 884.757 283.098 5.36579 0.002421
1.290 874.22 282.2 5.33933 0.00242
1.390 863.667 281.35 5.01376 0.002411
1.490 853.3 280.55 4.61738 0.00241
1.590 842.962 279.95 4.27487 0.002401
1.690 832.775 279.362 3.77744 0.002391
1.790 822.634 278.532 3.78002 0.00239
1.890 812.608 277.625 3.98339 0.002381
1.990 802.735 276.995 4.17061 0.00238
2.090 792.845 276.65 4.77151 0.002389
..
..

Note that the pattern that it repeat is $1 and I would like the first value that appear.

Thanks a lot and cheers . :D
 
sort -k 1,1 -u /path/to/your/datafile
will do the trick.

For more information, run

man sort
or
google for a "sort man page" if you don't have the man pages available on your system.

and read on ;-)



HTH,

p5wizard
 
Provided the file is sorted by the first column, you may try this:
Code:
awk '$1!=x{print;x=$1}' /path/to/input > output

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
In addition, the sort solution doesn't work, because the order in which it outputs same-key rows is arbitrary:

# head -1 dupfile
0.490 958.73 281.85 6.67985 0.002481
# awk '{if ($1!=x){print;x=$1}}' <dupfile|head -1
0.490 958.73 281.85 6.67985 0.002481 (OK)
# sort -k 1,1 -u dupfile|head -1
[red]0.490 954.833 283.991 8.73019 0.002471 (NOK)[/red]



HTH,

p5wizard
 
Thanks a lot for all reply.
It works correctly. Cheers for everybody. :D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top