AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 688277
Accepted
Lucas Djeu
Lucas Djeu
Asked: 2022-01-28 18:57:28 +0800 CST2022-01-28 18:57:28 +0800 CST 2022-01-28 18:57:28 +0800 CST

从多个文件中平均第 n 行到一个平均主文件

  • 772

我有 3 个文件,其中包含 8 行数值和文本。我试图取所有三个文件中每一行的平均值,并用这些平均值打印一个新文件。下面是三个示例文件,都是同名格式 testfile1.1, testfile1.2, testfile1.3

测试文件1.1

1
2048
8
5
5
4
9
Lat:1

测试文件1.2

1
2048
10
7
7
4
9
Lat:1

测试文件1.3

1
2048
3
6
3
4
6
Lat:7

我希望输出文件如下所示(取平均值后)

平均文件1

1
2048
7
6
5
4
8
Lat:3

希望这对我正在尝试做的事情有意义!

我尝试过使用 awk、sed 的不同组合,它们对 3-4 行数据运行良好,但我的实际数据在 40 多个文件名中有 2000 多行

编辑:所以我能够理解如何控制我想要打印的 sig figs 以及如何编辑正则表达式以更好地匹配浮动小数。

(请让我知道我是否应该将此作为另一个问题并删除此问题!)。

我的实际数据有很多其他行,其中包含文本以及我想要取平均值的值。我试图创建额外的字符串,但后来变得更加困惑。在我的真实文件中,在某些行上,我想要不同的命令,例如从行中打印文本,对实际数据取平均值,复制文本的行和数据的平均值以及日期和时间的平均值。

下面是 2 个文件的副本(每行都有我想对它们做的评论)。

豆腐1.1

ABCDEFGH #print text into output file (same on both files)
1     # Take average of values across all the files in this line
2048  # Take average of values across all the files in this line
8     # Take average of values across all the files in this line
5     # Take average of values across all the files in this line
5     # Take average of values across all the files in this line
4     # Take average of values across all the files in this line
9.5   # Take average of values across all the files in this line
1     # Take average of values across all the files in this line
90.00  # Check and make sure value in this line across print if same
Sprite # check and see if text is same across all values and print if same
cats10   # check and see if text is same across all values and print if same
07/02/20 # See below for explantion on next 3 lines
08:32
08:32
290.000000 # average across all 3 files on this line
10.750000 # average across all 3 files on this line
SCANS23   # output should be SCANS "average of values"
INT_TIME57500 # output should be INT_TIME with sum of all values
SITE northpole   #Check if all lines are same if so print line
LONGITUDE -147.850037  # Output should be LONGITUDE%f
LATITUDE 64.859375     # Output should be LONGITUDE%f

第 13 行是数据的来源日期,第 14 行是开始时间和结束时间。可能使用某种日期到十进制命令..有没有办法取日期的平均值?如果一个数据是在 2020 年 7 月 2 日获取的,而另一个数据是在 2018 年 7 月 2 日获取的,那么输出可以是 19 年 7 月 2 日吗?时间的平均值也会被考虑在内。

豆腐1.2

ABCDEFGH #print text into output file (same on both files)
1     # Take average of values across all the files in this line
2048  # Take average of values across all the files in this line
10    # Take average of values across all the files in this line
7     # Take average of values across all the files in this line
7     # Take average of values across all the files in this line
4     # Take average of values across all the files in this line
8   # Take average of values across all the files in this line
1     # Take average of values across all the files in this line
90.00  # Check and make sure value in this line across print if same
Sprite # check and see if text is same across all values and print if same
cats10   # check and see if text is same across all values and print if same
07/02/20 # See below for explanation on next 3 lines
08:32
08:32
290.000000 # average across all 3 files on this line
10.750000 # average across all 3 files on this line
SCANS23   # output should be SCANS "average of values"
INT_TIME57500 # output should be INT_TIME with sum of all values
SITE northpole   #Check if all lines are same if so print line
LONGITUDE -147.850037  # Output should be LONGITUDE%f
LATITUDE 64.859375     # Output should be LONGITUDE%f

我厌倦了尝试在脚本中包含多个字符串起始值的尝试,但很快就变得非常混乱。

awk -F: '
  FNR==1     { c++ };
  /^LATITUDE/    { a[FNR] += $6 };
  /^SCANS/    { a[FNR] += $2 };
  /^[+-]?([0-9]*[.])?[0-9]+$/ { a[FNR] += $1 };

  END {
    for (i in a) {
      printf (i==22 ? "LATITUDE%f": i==18 ? "SCANS%2.3f": "%f") "\n", a[i] / c
    }
  }' tofu1.* > askforhelp

这给了我

$ more askforhelp

90.000000
LATITUDE0.000000
290.000000
10.750000
SCANS0.000
1.000000
2048.000000
6.333333
4.666667
5.000000
4.000000
7.833333
2.666667

我还尝试一次添加多个文本字符串,当我完全没有从这次尝试中得到输出时,我感到非常困惑。

awk -F: '
  FNR==1     { c++ };
  /^LATITUDE/    { a[FNR] += $6 };
  /^LONGITUDE/    { a[FNR] += $5 };
  /^SITE/    { a[FNR] += $4 };
  /^INT_TIME/    { a[FNR] += $3 };
  /^SCANS/    { a[FNR] += $2 };
  /^[+-]?([0-9]*[.])?[0-9]+$/ { a[FNR] += $1 };

  END {
    for (i in a) {
      printf (i==22 ? "LATITUDE%f": 
              i==21 ? "LONGITUDE%2.3f": 
              i==20 ? "SITE%2.3f": 
              i==19 ? "INT_TIME%2.3f": 
              i==18 ? "SCANS%2.3f": "%f") "\n", a[i] / c 
    }
  }' /home/lmdjeu/test/test1.* > /home/lmdjeu/test/askforhelp

data math
  • 1 1 个回答
  • 72 Views

1 个回答

  • Voted
  1. Best Answer
    cas
    2022-01-28T21:10:08+08:002022-01-28T21:10:08+08:00
    $ awk -F: '
      FNR==1     { c++ };
      /^Lat:/    { a[FNR] += $2 };
      /^[0-9]+$/ { a[FNR] += $1 };
    
      END {
        for (i in a) {
          printf (i==8 ? "Lat:%i" : "%i") "\n", a[i] / c
        }
      }' Testfile1.* > Averagefile1
    
    $ cat Averagefile1
    1
    2048
    7
    6
    5
    4
    8
    Lat:3
    

    这使用变量c来计算它已读入的文件数。c每当读取文件的第一行 ( FNR==1) 时都会递增。 FNR是 awk 自动设置的输入记录(行号)计数器,每次读取输入文件时都会重置。

    它还使用一个数组a来存储每行输入的累积和 -FNR用作数组的索引。如果该行仅包含数字,则将该行的第一个(也是唯一的字段)添加到该行的数组元素中。如果它以 string 开头Lat:,则添加第二个字段。

    一旦读取并处理了所有输入文件,就执行 END 块。这会遍历数组,打印每个元素的总和除以文件数。除了第 8 行之外的所有内容都仅打印为整数。

    对于第 8 行,整数以字符串 为前缀Lat:。该脚本为此使用 awk 的三元运算符:condition ? result_if_true : result_if_false

    • 1

相关问题

  • 从 JSON 字符串中提取数据

  • 在不读取数据的情况下验证磁盘完整性?

  • 安全的临时存储

  • 在 awk 脚本中使用 if 和 shell 变量

  • 带有 PC 跟踪机制的 Ubuntu 18.04 仍然是“免费软件”?[关闭]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve