文件 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 年初至今
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%
)
)
)
最佳答案
像这样:
awk -F':' 'NR==FNR{a[NR]=$0;next}{print a[FNR]$2}' mnt ytd
- header 被忽略。
- 假设两个文件具有相同的行数。
关于awk:将 2 个 csv 文件合并为 1 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35289455/