Comparing two columns

Hi,
I want to compare two columns and find out missing entries e:g

Column 1    Column 2
1                   1
2                   2
3                   13               
4
10
19
234  

Results woud be 13. I will appreciate very much if anyone help me :).

awk '$NF != $(NF-1){print $NF}' file
perl -lane 'print $F[1] if ( $F[1] && $F[1] != $F[0])'

Or:

awk '$0=$1==$2?0:$2' infile

Else Or:

perl -ane '$F[1]!=$F[0]&&print$F[1]' file

Or:

perl -lane'print$F[1]if$F[1]&&!/(\S+)\s+\1$/' infile

Edit: Re-reading the initial question, I realized that the OP didn't specify how multiple values should be displayed :slight_smile:
So I'm just guessing ...

I pretty much meant what I posted, but this will add a new line after every match, which is probably a good thing:

perl -ane '$F[1]!=$F[0]&&$F[1]&&print$F[1],"\n"' dat

Yes, and you can use the -l option for that, like you did i your first post to this thread.

I mean this:

% perl -lane '$F[1]!=$F[0]&&print$F[1]' infile        
13





% 

vs. this:

% perl -lane'print$F[1]if@F[1]&&!/(\S+)\s+\1$/' infile
13
%

Sorry, if it was not clear.

You are mixing and matching my posts.

The second post, the one you are referring to, omits the -l flag, which prevents the problem with the extra output lines, as you show there. you include the -l flag, hence the problem.

Try it exactly the way I posted it, with -ane

perl -ane '$F[1]!=$F[0]&&print$F[1]' file

Pretty sure you do get not all the empty lines, that way.... let me know.

I think the third attempt is best anyhow.

Thanks, and happy Perlin'

Scott

Hi Scott,

You're right, I was not clear at all.

Correct, you get the value (and eventually the values concatenated together with no separator) without the trailing newline character.

Yes,
with the original sample I get this:

% perl -ane '$F[1]!=$F[0]&&print$F[1]' infile
13% 

With the test input file I was using (more than one record with mismatching fields) I got:

% cat infile 
1                   1
2                   2
3                   13               
4
10
19                  1
234
% perl -ane '$F[1]!=$F[0]&&print$F[1]' infile
131%  

Correct, sorry for the noise ...

Better than this?

perl -lane '$F[1]!=$F[0]&&$F[1]&&print$F[1]' infile

Regards
Dimitre

When I say "my third attempt" I mean the one-liner shown here, it accounts for duplicates, extra white space and outputs the line feed after every match.

[root@localhost cols]# cat dat
1 1
2 2
3   13
4
10
19
234
3  14
4   4
5 5
112 13
[root@localhost cols]# perl -ane '$F[1]!=$F[0]&&$F[1]&&print$F[1],"\n"' dat
13
14
13

I guess when you add the -l back in, the "\n" becomes unnecessary, so: no, not better than "that". You are correct :slight_smile:

Thanks for your reply. My sample input was not clear. Duplicate values can occur anywhere in the 2nd column than side by side. e:g

10    1
1      4
2      13
3
4 
93

So how the output should look like?