问题
The output from a command is sent as a CSV list of UUIDs. The UUIDs are not sorted however, so it's very difficult to tell if a line is unique. I would like to sort each line by the value between the commas, and then uniq
the lines.
I know I could hack something up with awk
, but I was hoping for a cleaner/more elegant one-liner. Any ideas?
EDIT
Here is some sample data:
9166e19c-4794-467e-baad-3f8c2f2656cb,f5553f54-589b-4afd-a8e0-2239b23dc138,ee721e70-a7e2-4da2-a2b0-22bec3432c3d,7e17bf09-e56b-428e-94c9-a7dc50991e00,360b7de7-d7e5-455a-8eb8-0bd856c705ed
9166e19c-4794-467e-baad-3f8c2f2656cb,360b7de7-d7e5-455a-8eb8-0bd856c705ed,7e17bf09-e56b-428e-94c9-a7dc50991e00,f5553f54-589b-4afd-a8e0-2239b23dc138,ee721e70-a7e2-4da2-a2b0-22bec3432c3d
9166e19c-4794-467e-baad-3f8c2f2656cb,ee721e70-a7e2-4da2-a2b0-22bec3432c3d,7e17bf09-e56b-428e-94c9-a7dc50991e00,f5553f54-589b-4afd-a8e0-2239b23dc138,360b7de7-d7e5-455a-8eb8-0bd856c705ed
9166e19c-4794-467e-baad-3f8c2f2656cb,ee721e70-a7e2-4da2-a2b0-22bec3432c3d,360b7de7-d7e5-455a-8eb8-0bd856c705ed,7e17bf09-e56b-428e-94c9-a7dc50991e00,f5553f54-589b-4afd-a8e0-2239b23dc138
9166e19c-4794-467e-baad-3f8c2f2656cb,f5553f54-589b-4afd-a8e0-2239b23dc138,360b7de7-d7e5-455a-8eb8-0bd856c705ed,ee721e70-a7e2-4da2-a2b0-22bec3432c3d,7e17bf09-e56b-428e-94c9-a7dc50991e00
9166e19c-4794-467e-baad-3f8c2f2656cb,360b7de7-d7e5-455a-8eb8-0bd856c705ed,ee721e70-a7e2-4da2-a2b0-22bec3432c3d,f5553f54-589b-4afd-a8e0-2239b23dc138,7e17bf09-e56b-428e-94c9-a7dc50991e00
9166e19c-4794-467e-baad-3f8c2f2656cb,7e17bf09-e56b-428e-94c9-a7dc50991e00,f5553f54-589b-4afd-a8e0-2239b23dc138,360b7de7-d7e5-455a-8eb8-0bd856c705ed,ee721e70-a7e2-4da2-a2b0-22bec3432c3d
9166e19c-4794-467e-baad-3f8c2f2656cb,360b7de7-d7e5-455a-8eb8-0bd856c705ed,ee721e70-a7e2-4da2-a2b0-22bec3432c3d,f5553f54-589b-4afd-a8e0-2239b23dc138,7e17bf09-e56b-428e-94c9-a7dc50991e00
9166e19c-4794-467e-baad-3f8c2f2656cb,f5553f54-589b-4afd-a8e0-2239b23dc138,360b7de7-d7e5-455a-8eb8-0bd856c705ed,7e17bf09-e56b-428e-94c9-a7dc50991e00,ee721e70-a7e2-4da2-a2b0-22bec3432c3d
9166e19c-4794-467e-baad-3f8c2f2656cb,7e17bf09-e56b-428e-94c9-a7dc50991e00,360b7de7-d7e5-455a-8eb8-0bd856c705ed,f5553f54-589b-4afd-a8e0-2239b23dc138,ee721e70-a7e2-4da2-a2b0-22bec3432c3d
9166e19c-4794-467e-baad-3f8c2f2656cb,7e17bf09-e56b-428e-94c9-a7dc50991e00,ee721e70-a7e2-4da2-a2b0-22bec3432c3d,360b7de7-d7e5-455a-8eb8-0bd856c705ed,f5553f54-589b-4afd-a8e0-2239b23dc138
9166e19c-4794-467e-baad-3f8c2f2656cb,ee721e70-a7e2-4da2-a2b0-22bec3432c3d,7e17bf09-e56b-428e-94c9-a7dc50991e00,360b7de7-d7e5-455a-8eb8-0bd856c705ed,f5553f54-589b-4afd-a8e0-2239b23dc138
9166e19c-4794-467e-baad-3f8c2f2656cb,f5553f54-589b-4afd-a8e0-2239b23dc138,360b7de7-d7e5-455a-8eb8-0bd856c705ed,7e17bf09-e56b-428e-94c9-a7dc50991e00,ee721e70-a7e2-4da2-a2b0-22bec3432c3d
9166e19c-4794-467e-baad-3f8c2f2656cb,360b7de7-d7e5-455a-8eb8-0bd856c705ed,7e17bf09-e56b-428e-94c9-a7dc50991e00,f5553f54-589b-4afd-a8e0-2239b23dc138,ee721e70-a7e2-4da2-a2b0-22bec3432c3d
Cheers.
回答1:
With Perl and uniq
you can do it like this:
perl -F, -lane '@A=sort @F; print join(",",@A)' input_file | uniq
EDIT:
@A
is actually unneccessary, this works as well and is shorter and faster.
perl -F, -lane 'print join(",",sort @F)' input_file | uniq
Options used:
-e
= may be used to enter one line of program (one liner mode)-a
= turn on autosplit mode, puts values from split on @F array-F,
= set split delimiter to comma-n
= causes perl to assumewhile(<>){...}
loop around your program-l
= enables automatic line-ending processing
In this line @F
is a special array containing UUIDs that were split. It's sorted and copied to @A
array. Then @A
is printed with values being joined by ,
. Output from this command is piped to uniq
so you get unique lines only.
Output:
360b7de7-d7e5-455a-8eb8-0bd856c705ed,7e17bf09-e56b-428e-94c9-a7dc50991e00,9166e19c-4794-467e-baad-3f8c2f2656cb,ee721e70-a7e2-4da2-a2b0-22bec3432c3d,f5553f54-589b-4afd-a8e0-2239b23dc138
回答2:
using shell
while read i
do
echo $i | tr "," "\n" | sort | tr "\n" "," | sed "s/,$//"
echo
done < file |sort -u
来源:https://stackoverflow.com/questions/21468143/sort-a-csv-row-in-bash