【问题标题】:How can I order a text file with specific criteria?如何订购具有特定条件的文本文件?
【发布时间】:2019-11-15 05:05:30
【问题描述】:

我无法解决文本文件的问题,因为我是 linux 命令和/或 bash 脚本的初学者。

我有一个这样的文本文件:

object1 10.603  0.757
object1 10.523  0.752
object1 10.523  0.752
object1 10.456  0.747
object1 10.456  0.747
object1 10.271  0.734
object2 11.473  0.194
object2 11.460  0.194
object2 11.445  0.191
object2 11.421  0.190
object3 9.272   0.12
object3 9.236   0.12
object3 8.814   0.119
object3 0.968   0.119
object3 10.959  0.119

我必须在这个文件上做一个特定的切割和排序操作:对于每个包含单词“object1”、“object2”等的字符串,我只想打印具有最高值的字符串第三栏;然后我想根据第三列的值对该操作的输出进行排序。

为了清楚起见,输出应该是这样的:

object1 10.603  0.757
object2 11.473  0.194
object3 9.272   0.12

对使用 linux 命令和/或 bash 脚本有什么建议吗?

谢谢大家

【问题讨论】:

    标签: linux bash awk command text-files


    【解决方案1】:

    使用sortawk

    sort -k1,1 -k3rn -k2rn file.txt | awk '!seen[$1] {print} {seen[$1]++}'
    

    sort 首先对第一个字段进行排序,然后对第三个字段进行反向排序,然后对第二个字段进行反向排序(如果无关紧要,可以省略后者)。然后awk 仅打印仅考虑第一个字段发现的第一个唯一行。

    【讨论】:

      【解决方案2】:

      awk 中的一个:

      $ awk '{
          if(m[$1]<$3) {   # if previous max for 1st field val is bigger
              m[$1]=$3     # replace max value
              r[$1]=$0     # store record
          }
      }
      END {                # in the end
          for(i in r)      # iterate hashed records
              print r[i]   # and output
      }' file
      

      输出(不分特定顺序,如果需要排序,请在END{} 块的开头使用sort 或带有PROCINFO["sorted_in"]="@ind_str_asc" 的GNU awk):

      object1 10.603  0.757
      object2 11.473  0.194
      object3 9.272   0.12
      

      更新

      另一个使用sortuniqshuf仅用于演示:

      $ sort -k1r -k3n <(shuf file) | uniq -w 7
      object3 9.272   0.12
      object2 11.473  0.194
      object1 10.603  0.757
      

      为了对第一个字段进行分组,我使用了:(man uniq):

      -w, --check-chars=N 在行中比较不超过 N 个字符

      【讨论】:

        【解决方案3】:

        这是完成这项工作的另一个 awk 脚本。

        script.awk

        $1 == currObj{    # for each reoccouring object
            if ( ($3 + 0) > maxArr[$1] ) maxArr[$1] = $3 + 0;  # identify the max and store in maxArr
            next;         # skip to read next line
        }
        {                 # for each line having new object
            currObj = $1; # store current object in 1st field into variable currObj
            maxArr[$1] = $3; # reset the maxArr to current value
            fld2Arr[$1] = $2; # store 2nd field into an array;
        }
        END {             # post processing
            for (i in maxArr) print i, fld2Arr[i], maxArr[i]; # print for each index the array values
        }
        

        运行:

        awk -f script.awk input.txt
        

        输出:

        object1 10.603 0.757
        object2 11.473 0.194
        object3 9.272 0.12
        

        【讨论】:

          【解决方案4】:

          在排序前使用 awk 过滤数据。

          awk 'a[$1] < $3 {a[$1] = $3; b[$1]=$0} END {for (i in a) print b[i]}' input | sort -k3rn
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-02-17
            • 1970-01-01
            • 2018-03-23
            • 1970-01-01
            • 2020-07-04
            • 2021-07-10
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多