zoukankan      html  css  js  c++  java
  • linux 系统中awk命令实现统计每行数据的最大值、最小值

    1、测试数据

    root@PC1:/home/test# ls
    test.txt
    root@PC1:/home/test# cat test.txt
    3 4 2 2
    1 9 5 7
    5 7 8 4
    2 3 4 6

    2、统计每行数据的最大值

    root@PC1:/home/test# ls
    test.txt
    root@PC1:/home/test# cat test.txt
    3 4 2 2
    1 9 5 7
    5 7 8 4
    2 3 4 6
    root@PC1:/home/test# awk '{for(i = 2; i <= NF; i++) {if($i < $(i - 1)) {$i = $(i - 1)}} {print $NF}}' test.txt  ##输出每行数据的最大值
    4
    9
    8
    6

    3、输出每行数据的最小值

    root@PC1:/home/test# ls
    test.txt
    root@PC1:/home/test# cat test.txt
    3 4 2 2
    1 9 5 7
    5 7 8 4
    2 3 4 6
    root@PC1:/home/test# awk '{for(i = 2; i <= NF; i++) {if($i > $(i - 1)) {$i = $(i - 1)}} {print $NF}}' test.txt  ## 输出每行数据中的最小值
    2
    1
    4
    2

    4、R语言实现

    list.files()
    dat <- read.table("test.txt", header = F)
    dat
    apply(dat, 1, max)
    apply(dat, 1, min)
    > list.files()
    [1] "test.txt"
    > dat <- read.table("test.txt", header = F)   ## 读取测试数据
    > dat
      V1 V2 V3 V4
    1  3  4  2  2
    2  1  9  5  7
    3  5  7  8  4
    4  2  3  4  6
    > apply(dat, 1, max)  ## 输出每行数据的最大值
    [1] 4 9 8 6
    > apply(dat, 1, min)  ## 输出每行数据的最小值
    [1] 2 1 4 2
  • 相关阅读:
    BestCoder Round #87 1001
    p1304 家族
    hdu 1003
    hdu 1231
    hdu 1232 畅通工程
    hdu 4107
    Robot Framework--用例、数据、流程分离例子
    Robot Framework--RIDE面板与库的说明
    Robot Framework--pybot命令
    Robot Framework--运行pybot时出错
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15805641.html
Copyright © 2011-2022 走看看