linux - AWK:从另一个文件添加新字段

标签 linux bash loops awk

我有一个用“@”分隔的文件。它具有可用于将文件分成几部分的重复数据。在另一个文件中,我有数据想作为另一列添加到第一个文件中。添加的数据源将与第一个文件中重复出现的数据的每个实例一起循环。文件如下所示:

文件 1

Race1@300Yards@6
Race2@300Yards@7
Race3@250Yards@7
Race4@250Yards@7
Race5@250Yards@8
Race6@250Yards@9
Race7@300Yards@10
Race8@300Yards@12
Race1@330Yards@10
Race2@300Yards@10
Race3@300Yards@10
Race4@300Yards@10
Race5@11/2Miles@11
Race6@7Miles@9
Race7@6Miles@8
Race8@51/2Miles@7
Race9@1Mile@8
Race10@51/2Miles@12
Race1@61/2Miles@6
Race2@11/16Miles@9
Race3@1Mile@9
Race4@11/2Miles@6
Race5@11/16Miles@10
Race6@1Mile@10
Race7@11/16Miles@12
Race8@1Mile@12

另一个文件看起来像:

文件 2

London
New York
Dallas

期望的结果如下:

Race1@300Yards@6@London
Race2@300Yards@7@London
Race3@250Yards@7@London
Race4@250Yards@7@London
Race5@250Yards@8@London
Race6@250Yards@9@London
Race7@300Yards@10@London
Race8@300Yards@12@London
Race1@330Yards@10@New York
Race2@300Yards@10@New York
Race3@300Yards@10@New York
Race4@300Yards@10@New York
Race5@11/2Miles@11@New York
Race6@7Miles@9@New York
Race7@6Miles@8@New York
Race8@51/2Miles@7@New York
Race9@1Mile@8@New York
Race10@51/2Miles@12@New York
Race1@61/2Miles@6@Dallas
Race2@11/16Miles@9@Dallas
Race3@1Mile@9@Dallas
Race4@11/2Miles@6@Dallas
Race5@11/16Miles@10@Dallas
Race6@1Mile@10@Dallas
Race7@11/16Miles@12@Dallas
Race8@1Mile@12@Dallas

我知道 awk 可用于按“Race1”拆分比赛地点。我认为它始于这样的事情:

awk '/Race1/{x="Race"++i;}{print $5= something relating to file 2}

有人知道如何使用 awk 或任何其他 Linux 命令使用循环和条件来解析两个文件吗?

最佳答案

如果将其保存为.awk

BEGIN {
    FS  = OFS = "@"
    i = 0 
    j = -1
}
NR == FNR {
    a[i++] = $1        
}
NR != FNR {
    if ($1 == "Race1") 
        j++        
    $4 = a[j]
    print       
}

然后运行

awk -f a.awk file2 file1

你会得到你想要的结果。

输出

Race1@300Yards@6@London
Race2@300Yards@7@London
Race3@250Yards@7@London
Race4@250Yards@7@London
Race5@250Yards@8@London
Race6@250Yards@9@London
Race7@300Yards@10@London
Race8@300Yards@12@London
Race1@330Yards@10@New York
Race2@300Yards@10@New York
Race3@300Yards@10@New York
Race4@300Yards@10@New York
Race5@11/2Miles@11@New York
Race6@7Miles@9@New York
Race7@6Miles@8@New York
Race8@51/2Miles@7@New York
Race9@1Mile@8@New York
Race10@51/2Miles@12@New York
Race1@61/2Miles@6@Dallas
Race2@11/16Miles@9@Dallas
Race3@1Mile@9@Dallas
Race4@11/2Miles@6@Dallas
Race5@11/16Miles@10@Dallas
Race6@1Mile@10@Dallas
Race7@11/16Miles@12@Dallas
Race8@1Mile@12@Dallas

解释

我们首先将输入和输出字段分隔符设置为 @。我们还初始化了将用作数组索引的变量 i, j

第一个条件检查我们是否正在使用 NR == FNR 遍历 file2。在第一个 block 中,我们将索引 i 与第一个字段相关联,即城市名称。然后我们递增 i

第二个条件检查我们是否正在使用 NR != FNR 遍历 file2。如果第一个字段等于 Race1,则我们增加 j(注意我们将 j 初始化为 -1)。我们将第 4 个字段设置为 a[j],然后打印该行。

关于linux - AWK:从另一个文件添加新字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30697744/

相关文章:

java - 使用 Linux 还是 Windows 为 iPhone 构建应用程序?

linux - 使用 mod_jk 配置 Apache2 和 tomcat7

php - 使用 glob() 显示目录中的图像,同时回显唯一的第一张图像

node.js - JS 闭包、Redis、循环、Async::空数组

Python - 循环遍历列表并在替换之前保存状态

c - Linux,无法从串口读取

linux - ubuntu 中的 etc/profile 文件意外结束

bash - 是否可以指定执行 getopts 条件的顺序?

bash - 用ffmpeg替换字幕中的单词

linux - Grep 来自文件的输入和基于搜索的列求和