简体   繁体   English

我需要找到最大值和最小值 Rowwise

[英]I need to find Maximum and Minimum value Rowwise

operator1,4578
operator2,1234
operator3,9875
operator4,6357
operator1,6353
operator4,9765
operator1,6347
operator1,6798
operator3,6793
operator3,1465
operator4,6796
operator4,3333

I need to find out minimum and maximum value of each operator using awk and python (need command in awk as well in Python too) and output will be as below :我需要使用awkpython找出每个运算符的最小值和最大值(也需要awk命令以及Python命令),输出如下:

Name        Min     Max
operator1   4578    6798 
operator2   1234    
operator3   1465    9875 
operator4   3333    9765 

Can anyone help me out please谁能帮我一下

@amit kumar: Try: @amit kumar:尝试:

awk -F, 'FNR==NR{A[$1]=A[$1]>$NF?A[$1]:$NF;B[$1]=B[$1]>$NF?$NF:(B[$1]?B[$1]:$NF);next} FNR==1 && FNR!=NR{print "Name Min Max"}($1 in A){print $1 "\t" B[$1] "\t" A[$1];delete B[$1];delete A[$1]}'  Input_file  Input_file

Here I am making field separator as a "," then FNR==NR condition will make sure that it gets executed only when first time Input_file is being read.在这里,我将字段分隔符设为“,”,然后 FNR==NR 条件将确保它仅在第一次读取 Input_file 时执行。 where FNR and NR both are awk's in-built keywords and define the line numbers in it.其中 FNR 和 NR 都是 awk 的内置关键字并定义其中的行号。 FNR's value will be RESET whenever a new Input_file being read where N's value will be keep on increasing till all the files being read successfully.每当读取新的 Input_file 时,FNR 的值将被重置,其中 N 的值将继续增加,直到成功读取所有文件。 Then in that block I am creating an array named A whose index is $1 and where I am checking if previous A[$1]'s value is greater than current same index of array compare to $NF then change A[$1]'s value to current $NF as keep it as it is(this is basically for having maximum values for each $1 of line), same thing I am doing to get the MINIMUM values for $1 in Input_file and storing values into array B. next will be skipping all the next coming statements so only FNR==NR condition will be executed.然后在那个块中,我创建了一个名为 A 的数组,其索引为 $1,并在其中检查前一个 A[$1] 的值是否大于当前相同的数组索引与 $NF 相比,然后更改 A[$1] 的值到当前 $NF 保持原样(这基本上是为了让每行 $1 具有最大值),我正在做的事情是在 Input_file 中获取 $1 的最小值并将值存储到数组 B 中。接下来将跳过所有接下来的语句,因此只会执行 FNR==NR 条件。 Now FNR==1 && FNR!=NR condition will be only TRUE when second file's 1st line is being read because before writing actual output I need to write the heading columns.现在 FNR==1 && FNR!=NR 条件将仅在读取第二个文件的第一行时为 TRUE,因为在写入实际输出之前我需要写入标题列。 ($1 in A) now checking if current line's $1 is present in array A then print the current $1 and array A's value and then array B's value as per OP's request. (A 中的 $1)现在检查当前行的 $1 是否存在于数组 A 中,然后根据 OP 的请求打印当前 $1 和数组 A 的值,然后打印数组 B 的值。

EDIT: Adding a non-one liner form of solution too now.编辑:现在也添加一种非单衬形式的解决方案。

awk -F, 'FNR==NR{
                 A[$1]=A[$1]>$NF?A[$1]:$NF;
                 B[$1]=B[$1]>$NF?$NF:(B[$1]?B[$1]:$NF);
                 next
                }
         FNR==1 && FNR!=NR{
                                print "Name Min Max"
                          }
         ($1 in A){
                        print $1 "\t" B[$1] "\t" A[$1];
                        delete B[$1];
                        delete A[$1]
                  }
        '    Input_file  Input_file

You can try this:你可以试试这个:

awk -F, '
   min[$1]>$2||!min[$1]{
      min[$1]=$2
   } 
   max[$1]<$2{
      max[$1]=$2
   } 
   END{
     for(i in max){
        print i,min[i],(max[i]!=min[i]?max[i]:"")
     }
   }' file

This script make used of 2 array min and max which are filled with values while parsing the input file.该脚本使用了 2 个数组minmax ,它们在解析输入文件时填充了值。

When parsing is finished, both array are printed.解析完成后,打印两个数组。

Note that max[i]!=min[i]?max[i]:"" is skipping the max value in case this is the same as the min one.请注意, max[i]!=min[i]?max[i]:""正在跳过最大值,以防它与最小值相同。

I let you put the header line, and play with column command in case you want indentation...我让你放置标题行,并在你想要缩进的情况下使用column命令......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有双值键的Python字典,需要查找最大值和最小值 - Python Dictionary with dual values key, need to find maximum and minimum value 查找文本文件的最小值,最大值和平均值 - Find minimum, maximum, and average value of a text file 查找元组中最小元素的最大值 - Find maximum value of minimum elements in tuple python 找到子树的最小值和最大值 - python find sub tree minimum and maximum value 您好,我需要创建一个 python 查询 select 在所选列的每一行中的最大值和最小值 - Hello, I need to create a python query that select a maximum and minimum value in every row of the selected columns 如何找到字典中每个键的最小值/最大值? 以及如何找到每个键的值的数量? - How do I find the minimum/maximum value for each key in a dictionary? And how do I find the number of values for each key? 在列表字典的每个索引处查找最小值和最大值 - Find minimum and maximum value at each index of a dictionary of lists 如何在元组列表中查找用户定义的最小值和最大值 - How to find user-defined minimum and maximum value in a list of tuples 如何在二叉树中找到最小值和最大值 - How to find the minimum, and maximum value within a Binary Tree 我将最后一个值作为最大值和最小值 - I'm getting the last value as the maximum and minimum value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM