Compare 2 colums in two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare 2 colums in two files
# 1  
Old 05-06-2014
Compare 2 colums in two files

Hi ,

I am trying to write a part in shell script. I have two files with one column each and would like to output it to a new file giving the records which are different.Pls help experts.

HTML Code:
File1
Column name
11
12
13
14
18
 
File2
Column name
11
12
14
17
19
20
 
File 3
Column Name Column name
13                   17
14                   19 
18                   20
 
# 2  
Old 05-06-2014
An awk approach:
Code:
awk '
        NR == FNR {
                A[$1]
                C[$1]
                next
        }
        {
                B[$1]
                C[$1]
        }
        END {
                for ( k in C )
                {
                        if ( !( k in A ) )
                                T[++i] = k
                        if ( !( k in B ) )
                                R[++j] = k
                }
                n = ( i > j ? i : j )
                for ( k = 1; k <= n; k++ )
                        print T[k], R[k]

        }
' OFS='\t' file1 file2

This User Gave Thanks to Yoda For This Post:
# 3  
Old 05-06-2014
If the files are sorted, try:
Code:
comm -3 file1 file2

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk on multiple colums

HI, I need to filter my data based on 3 different columns. Two of the columns are unique so it worked if u use something like grep 'PASS.*HIGH|HIGH.*PASS' file.tsv However the other column is not unique .. So basically I want to extract out lines that match only match these 3... (4 Replies)
Discussion started by: janshamsani
4 Replies

2. UNIX for Dummies Questions & Answers

Find the timestamp difference between two different colums in a file

Sample Input File 2014/10/09 CDE876172588765 00:09:45 00:10:10 200 200 11.7093 2014/10/09 CDE366134588757 01:04:34 01:04:54 210 210 9.8898 2014/10/09 CDE765172345745 03:05:46 03:06:01 100 100 10.0601 2014/10/09 ... (8 Replies)
Discussion started by: rpm120
8 Replies

3. Shell Programming and Scripting

Reading colums from a text file

Hi all, I have a text file that has (4) columns. There are about 300 lines on this file. It is a plain text file. I am looking to write a simple script that will read each line from the file and generate another text file. The file looks something like this: These are the columns: ... (4 Replies)
Discussion started by: adamw
4 Replies

4. Shell Programming and Scripting

Help on convert rows to colums

Need help to convert the following data Account name: admin Role: admin Description: Administrator Enabled: Yes to Account Name Role Description Enabled admin admin Administrator Yes Perl or AWK? Thanks San (9 Replies)
Discussion started by: sanguy
9 Replies

5. Shell Programming and Scripting

Rearranging the colums in a tab delimited file

I want to rearrange some of my columns in my dat file; how do i do this using a script Suppose, I have an input file like this: BASENAME STREETTYPE PREFIX SUFFIX HOUSENUMBER BUILDUP ORDER8 ORDER2 ORDER1 ISOCOUNTRYCODE POSTALCODE SILVER LAKE RD NW 1135 NEW BRIGHTON RAMSEY MINNESOTA USA 55112... (4 Replies)
Discussion started by: ramky79
4 Replies

6. Shell Programming and Scripting

Require compare command to compare 4 files

I have four files, I need to compare these files together. As such i know "sdiff and comm" commands but these commands compare 2 files together. If I use sdiff command then i have to compare each file with other which will increase the codes. Please suggest if you know some commands whcih can... (6 Replies)
Discussion started by: nehashine
6 Replies

7. Shell Programming and Scripting

Need to print last column based on the value of first 2 colums

Hi Everybody. I need an urgent help, it would be great if somebody will help me out in this regard. See below sample file 1525 805 26 2036 219644. 2598293. 1525 805 126 2327 1525 805 226 ... (6 Replies)
Discussion started by: syahmed
6 Replies

8. Shell Programming and Scripting

Compare two csv files by two colums and create third file combining data from them.

I've got two large csv text table files with different number of columns each. I have to compare them based on first two columns and create resulting file that would in case of matched first two columns include all values from first one and all values (except first two colums) from second one. I... (5 Replies)
Discussion started by: agb2008
5 Replies

9. Shell Programming and Scripting

merging colums from different files

hi all , i have three files that contain numbes as the first column . i want to add these files together so the numbers from the first file is in the first column ; the numbers from the second file in the second column; and the numbers from the third file in the third column . as an... (5 Replies)
Discussion started by: ppass
5 Replies

10. Shell Programming and Scripting

[Shell] How make colums in text file ??

hi, i have little pb, i would like make a colums, but my server not recongize "\t" or i write wrong.... and iam little noobs and no know awk... #!/bin/ksh #---------------------------------------------------------------------------- # Fichiers : ctrl.sh et ctrl2005.txt ... (6 Replies)
Discussion started by: parola
6 Replies
Login or Register to Ask a Question