Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'd like to compare the following 2 files and display a comparison of the 2 files with each other.

File #1:

DATE       DS 
2012-08-02 1
2013-06-23 1
2013-06-27 2
2013-06-28 2
2013-06-29 779

File #2:

DATE       DE
2013-06-16 5
2013-06-17 1
2013-06-18 3
2013-06-19 1
2013-06-20 5
2013-06-21 6
2013-06-22 6
2013-06-23 6
2013-06-24 5
2013-06-25 9
2013-06-26 7
2013-06-27 22
2013-06-28 59
2013-06-29 334
2013-06-30 11

Comparison of file #1 & #2:

DATE       DS   DE 
2012-08-02 -    1
2012-08-05 -    2
2013-06-16 5    -  
2013-06-17 1    -  
2013-06-18 3    -  
2013-06-19 1    -  
2013-06-20 5    -  
2013-06-21 6    -
2013-06-22 6    -
2013-06-23 6    -
2013-06-24 5    -
2013-06-25 9    -
2013-06-26 7    1
2013-06-27 22   2
2013-06-28 59   2
2013-06-29 334  779
2013-06-30 11   -

Above the file1, DATE and DS means "data selected" for a particular date of how many selected. It's showing from file1 and file2 also the same, but different dates for how many "deactivated" (DE).

Now I want to display from file1 & file2 using AWK.

Date for $1 from both files, $2 from file1, $2 from file2. If a particular date is compared also it will display DE, DA.

For example:

DATE      DE DS
2012-08-1 -   1 # Date is present in $1 from file1 but file2 is not there. 
                # This is the date I want. Also I want to display a (-) for 
                # DE which is not there.
share|improve this question
    
Welcome to unix.SE. Can you please clarify what it is that you're asking? –  Karlson Jul 17 '13 at 20:09
    
I've attempted to help you by describing what I thought you were trying to ask. Please update your question if I've not captured what you're trying to ask correctly. –  slm Jul 17 '13 at 20:12

1 Answer 1

Maybe:

join -a1 -a2 -o 0,1.2,2.2 -e - file1 file2

Description of above command

If on a GNU system (like most Linux-based distributions), you can find out the options for join with this command:

$ info coreutils 'join invocation'

or

$ info join

(assuming the info directory is properly maintained)

Or you can check the POSIX specification to see what's guaranteed to work across Unices.

The above options are as follows:

`-a FILE-NUMBER'
     Print a line for each unpairable line in file FILE-NUMBER (either
     `1' or `2'), in addition to the normal output.

That covers your requirement for lines not paired (where the date (the join field) doesn't appear in both) to be included.

`-o FIELD-LIST'
     Construct each output line according to the format in FIELD-LIST.
     Each element in FIELD-LIST is either the single character `0' or
     has the form M.N where the file number, M, is `1' or `2' and N is
     a positive field number.

This option constructs the format that will be displayed for each line of output from join. A 0 emits the field that is being matched between the 2 files. This is the date. The 1.2 is the 2nd column from the 1st file (file1) and the 2.2 is the 2nd column from the 2nd file (file2).

`-e STRING'
     Replace those output fields that are missing in the input with
     STRING.

This option specifies what character to use for fields that are missing from either file1 or file2. This is what generates the -, in the final output.

Using column

By making use of @GlennJackman's suggestion you can clean up the output further so that it's formatted nicely in equal sized columns:

$ join -a1 -a2 -o 0,1.2,2.2 -e - file1 file2 | column -t
DATE        DS   DE
2012-08-02  1    -
2013-06-16  -    5
2013-06-17  -    1
2013-06-18  -    3
2013-06-19  -    1
2013-06-20  -    5
2013-06-21  -    6
2013-06-22  -    6
2013-06-23  1    6
2013-06-24  -    5
2013-06-25  -    9
2013-06-26  -    7
2013-06-27  2    22
2013-06-28  2    59
2013-06-29  779  334
2013-06-30  -    11

Note that the input files must be sorted on the join key (by default the first field). Above, it's not since in most locales "DATE" sorts after "2013". So it's not guaranteed to work in all join implementations.

You could skip the first line with (ksh93/zsh/bash syntax):

join -a1 -a2 -o 0,1.2,2.2 -e - <(tail -n +2 file1) <(tail -n +2 file2)
share|improve this answer
    
join the two files. for a line having date common for two files .i want every date from file . i want compare the date also and $2 from file1,$2from file2. for example file 1 have date but $2 not have value or data ,that should be blank or (-). –  ganik Jul 17 '13 at 20:29
    
@ganik, that's what the -a option is for. Have you tried it? –  Stéphane Chazelas Jul 17 '13 at 20:30
1  
+1. Make the output pretty with column -t –  glenn jackman Jul 17 '13 at 22:08
    
@StephaneChazelas - I hope you don't mind but I expanded your answer since it's the best. Thanks for the example in using join. I never knew how to use it until today, thanks to you! –  slm Jul 18 '13 at 1:30
1  
For completeness' sake, maybe mention that join requires sorted input. –  tripleee Jul 18 '13 at 6:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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