1

I have three files, as shown below:

FILE 1: 
DATE      PGTO_CRED
20180801    50.00
20180802    150.00
20180803    130.00
20180804    110.00
20180805    200.00

FILE 2: 
DATE       PGTO_TOTAL
20180801    150.00
20180802    300.00
20180803    200.00
20180804    250.00
20180805    400.00

FILE 3: 
DATE       PGTO_FEE
20180801    35.00
20180802    10.00
20180803    25.00
20180804    140.00
20180805    135.00

I need my output file to be something like:

DATE    PGTO_CRED   PGTO_TOTAL  PGTO_FEE
20180801    50.00   150.00      35.00
20180802    150.00  300.00      10.00
20180803    130.00  200.00      25.00
20180804    110.00  250.00      140.00
20180805    200.00  400.00      135.00

How do I do this on Redhat Linux?

5

Many tools can do that, probably awk is the first which comes to mind, but I recommend the join command, especially if the input is already sorted (as in your example):

join file1 <(join file2 file3) | column -t

The column -t is just to nicely align the output, you can remove it.

Output:

DATE      PGTO_CRED  PGTO_TOTAL  PGTO_FEE
20180801  50.00      150.00      35.00
20180802  150.00     300.00      10.00
20180803  130.00     200.00      25.00
20180804  110.00     250.00      140.00
20180805  200.00     400.00      135.00
2

A couple of other options:

paste file{1,2,3} | awk '{print $1,$2,$4,$6}' | column -t

or just awk:

awk '
    NR == FNR {line[FNR] = $0; next} 
              {line[FNR] = line[FNR] OFS $2} 
    END       {for (i=1; i<=FNR; i++) print line[i]}
' file{1,2,3} | column -t

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.