Comparing two columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing two columns
# 8  
Old 01-01-2010
Quote:
Originally Posted by deindorfer
I pretty much meant what I posted, but this will add a new line after every match, which is probably a good thing
[...]
Yes, and you can use the -l option for that, like you did i your first post to this thread.

I mean this:
Code:
% perl -lane '$F[1]!=$F[0]&&print$F[1]' infile        
13





%

vs. this:

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

Sorry, if it was not clear.
# 9  
Old 01-01-2010
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

Code:
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
# 10  
Old 01-01-2010
Hi Scott,

Quote:
Originally Posted by deindorfer
You are mixing and matching my posts.
You're right, I was not clear at all.

Quote:
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.
Correct, you get the value (and eventually the values concatenated together with no separator) without the trailing newline character.

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

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

Yes,
with the original sample I get this:

Code:
% 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:

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

Quote:
Pretty sure you do get not all the empty lines, that way.... let me know.
Correct, sorry for the noise ...

Quote:
I think the third attempt is best anyhow.
Better than this?
Code:
perl -lane '$F[1]!=$F[0]&&$F[1]&&print$F[1]' infile



Regards
Dimitre
# 11  
Old 01-01-2010
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.

Code:
[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 Smilie

Last edited by deindorfer; 01-01-2010 at 10:05 PM.. Reason: Correction
# 12  
Old 01-02-2010
Quote:
Originally Posted by danmero
Code:
awk '$NF != $(NF-1){print $NF}' file

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

Code:
10    1
1      4
2      13
3
4 
93


Last edited by radoulov; 01-02-2010 at 01:30 PM.. Reason: Please use code tags!
# 13  
Old 01-02-2010
Quote:
Originally Posted by krabu
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

Code:
10    1
1      4
2      13
3
4 
93

So how the output should look like?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing two columns from two different files

Hi, I have a single-column file1 having records like: 00AB01/11 43TG22/00 78RC09/34 ...... ...... and a second file , file 2 having two columns like 78RC09/34 1 45FD11/11 2 00AB01/11 3 43TG22/00 4 ...... ...... (8 Replies)
Discussion started by: amarn
8 Replies

2. Shell Programming and Scripting

Comparing columns in a file

I have two files. One a small one and another one is big. The smaller one look like this: Filename: 1.tmp 3453 0 326543 1 2321 0 3212 1 The big file looks like this: Filename 1.res 0.3232 2321 9.2922 123 0.983 3212 8.373 326543 0.9 3453 1.098 3432 I want to extract those lines... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

3. Shell Programming and Scripting

Comparing rows and columns

Hi, i've a .csv file with the data as below: - file1.h, 2.0 file2.c, 3.1 file1.h, 2.5 file3.c, 3.3.3 file1.h, 1.2.3 I want to remove the duplicate file names considering only the one with the highest version number.. output should be file1.h, 2.5 file2.c, 3.1 file3.c,... (3 Replies)
Discussion started by: pravsripad
3 Replies

4. Shell Programming and Scripting

comparing two columns from two different files

Hello, I have two files as 1.txt and 2.txt with number as columns. 1.txt 0 53.7988 1 -30.0859 2 20.1632 3 14.2135 4 14.6366 5 -37.6258 . . . 31608 -8.57333 31609 -2.58554 31610 -24.2857 2.txt (1 Reply)
Discussion started by: AKD
1 Replies

5. UNIX for Dummies Questions & Answers

Comparing columns in two files

Hi, I have two files. File1.txt has 2 columns and looks like: 458739 122345 4456 122657 34200 122600 File2.txt has many columns with column 1 the same as column2 of File1.txt, but with lot more rows: 122786 abcdefg user1@email 122778 uuhjeufh user2@email... (1 Reply)
Discussion started by: ursaan
1 Replies

6. UNIX for Dummies Questions & Answers

Comparing 2 columns from 2 files

Hi, I have two files with the same number of columns. Basically I want to print the 2 columns that match between the two files. File1 looks like this: dr12 12 6 abn dr14 12 7 abn File2 looks something like this: dr12 12 8 abn dr12 14 7 abn So basically if the first... (1 Reply)
Discussion started by: kylle345
1 Replies

7. Shell Programming and Scripting

comparing 2 columns from 2 files

Hey, I have 2 files that have a name and then a number: File 1: dog 21 dog 24 cat 33 cat 27 dog 76 cat 65 File 2: dog 109 dog 248 cat 323 cat 207 cat 66 (2 Replies)
Discussion started by: dcfargo
2 Replies

8. Shell Programming and Scripting

comparing the columns in two files

I have two files file1 and file 2 both are having multiple coloumns.i want to select only two columns. i used following code to get the desired columns,with ',' as delimiter cut -d ',' -f 1,2 file1 | sort > file1.new cut -d ',' -f 1,2 file2 | sort > file2.new I want to get the coloums... (1 Reply)
Discussion started by: bab123
1 Replies

9. Shell Programming and Scripting

Comparing Columns of two FIles

Dear all, I have two files in UNIX File1 and File2 as below: File1: 1,1234,.,67.897,,0 1,4134,.,87.97,,4 0,1564,.,97.8,,1 File2: 2,8798,.,67.897,,0 2,8879,.,77.97,,4 0,1564,.,97.8,,1 I want to do the following: (1) Make sure that both the files have equal number of columns and if... (4 Replies)
Discussion started by: ggopal
4 Replies

10. UNIX for Advanced & Expert Users

Comparing Columns of two FIles

Dear all, I have two files in UNIX File1 and File2 as below: File1: 1,1234,.,67.897,,0 1,4134,.,87.97,,4 0,1564,.,97.8,,1 File2: 2,8798,.,67.897,,0 2,8879,.,77.97,,4 0,1564,.,97.8,,1 I want to do the following: (1) Make sure that both the files have equal number of columns and if... (1 Reply)
Discussion started by: ggopal
1 Replies
Login or Register to Ask a Question