简体   繁体   中英

How do I sort these values using Bash Script in Linux?

-53 45
-54 43
-55 42
-56 41
-57 40
-59 37
-61 35
-61 36
-62 34
-64 33
-65 31
-65 32
-67 30
-68 29
-69 28
-72 25
-73 23
-73 24
-74 22
-76 20
-76 22
-78 20
-79 18
-80 17
-81 16

In the above you will see that at -61 occurs twice and so do some other values. I want to just create a new file without any duplicates. So the new file should have either -61 35 or -61 36 ...

How do I do that?! I tried using sort using uniq but that didn't work.

Assuming your data is in a file called input

cat input | sort -u -n

When doing a numeric (-n) sort along with unique (-u), the duplicate checking is achieved.

If you can guarantee the length of the first field,

sort | uniq --check-chars=4 

will do the trick.

Otherwise, try awk:

awk '{ if (FNR == 1 || last != $1) print; last = $1; }'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM