awk:将2个csv文件合并为1个

时间:2016-02-09 10:21:15

标签: awk merge

档案1 MNT

title
from:,01.01.2016
to:,31.01.2016
days:,31
employees count:,169.00
counteddays:,206
counteddays KF:,141
counteddays KG:,65
percentage:,3.50%
percentage KF:,2.40%
percentage KG:,1.10%
results on:,08.02.2016

文件2 YTD

title
from:,01.01.2016
to:,08.02.2016
days:,39
employees count:,168.62
counteddays:,250
counteddays KF:,172
counteddays KG:,78
percentage:,3.38%
percentage KF:,2.33%
percentage KG:,1.06%
results on:,08.02.2016

我想得到什么:

title, month, ytd

from:,01.01.2016,01.01.2016
to:,31.01.2016,08.02.2016
days:,31,39
employees count:,169.00,168.62
counteddays:,206,250
counteddays KF:,141,172
counteddays KG:,65,78
percentage:,3.50%,3.38%
percentage KF:,2.40%,2.33%
percentage KG:,1.10%,1.06%
results on:,08.02.2016,08.02.2016

在另一个帖子上我找到了类似的东西:

awk 'FNR==NR{a[$1]=$2 FS $3;next}{ print $0, a[$1]}' file2 file1

但我无法正确获取文件的语法。

我有一个月度任务,以上面显示的格式提供2个csv文件。 对我来说,任务只是以简单的方式将它们与基本批处理工具或awk合并。

编辑:我试过的批处理方式,但不起作用:

for /f "tokens=1 delims=," %%a in ('type %MNT% ^| find ","') do (
  for /f "tokens=2 delims=," %%b in ('type %MNT% ^| find "%%a"') do (
    for /f "tokens=2 delims=," %%c in ('type %YTD% ^| find "%%a"') do (
        echo %%a,%%b,%%c >> %RESULT%
    )
  )
)

1 个答案:

答案 0 :(得分:1)

像这样:

awk -F':' 'NR==FNR{a[NR]=$0;next}{print a[FNR]$2}' mnt ytd
  • 标题被忽略。
  • 假设两个文件具有相同的行数。