ContraViZe
ContraViZe

Reputation: 121

awk: merge 2 csv files into 1

File 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

File 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

what i want to get:

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

on another thread i found something like this:

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

but i can't get the syntax right for my files.

i have a monthly task that gives me 2 csv files in the format shown above. The task for me is just to merge them in a simple way with either basic batch tools or awk.

Edit: the batch way i tried, but is not working:

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%
    )
  )
)

Upvotes: 2

Views: 130

Answers (1)

Kent
Kent

Reputation: 195039

like this:

awk -F':' 'NR==FNR{a[NR]=$0;next}{print a[FNR]$2}' mnt ytd
  • the header is ignored.
  • assume two files have same number of lines.

Upvotes: 2

Related Questions