本文介绍了排序在bash一个CSV行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从命令的输出被送到像UUID的CSV列表。的UUID不但是排序,所以这是很难判断一个行是独一无二的。我想排序的每行的由逗号之间的值,然后 uniq的行。

我知道我可以砍的东西了 AWK ,但我希望一个更清洁/更优雅的单行。任何想法?

修改

下面是一些样本数据:

<$p$p><$c$c>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

干杯。


解决方案

使用Perl和 uniq的你可以做到这样的:

  perl的-F,-lane'@ A =排序@F;打印连接(,,@ A)INPUT_FILE | uniq的

编辑:

@A 其实是不必要的,这工作以及和更短,速度更快。

  perl的-F,-lane打印连接(,,排序@F)INPUT_FILE | uniq的

使用选项:


  • -e =可用于进入程序的一行(单行模式)

  • -a =打开自动分割模式,把价值从分裂@F阵列上

  • -F =设置拆分分隔符逗号

  • -n =会让perl承担而(LT;&GT;)围绕{...} 循环你的程序

  • -l =启用自动行结束处理

在此行 @F 是包含被分开的UUID的一个特殊的数组。它的排序,并复制到 @A 阵列。然后 @A 印刷与被连接值,。所以你只能得到独特的线条从这个命令的输出管道输送到 uniq的

输出:

<$p$p><$c$c>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

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.

解决方案

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 assume while(<>){...} 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

这篇关于排序在bash一个CSV行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:01