Compare line and printing difference


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare line and printing difference
# 1  
Old 08-07-2015
Compare line and printing difference

Hi,

I want to compare two files and print out their differences
e.g:
t1.txt
Code:
a,b,c,d

t2.txt
Code:
a,b,c,d,e,f

Output
Code:
e,f

Currently I do this long about way
Code:
tr ',' '\n' <t1.txt >t1.tmp
tr ',' '\n' <t2.txt >t2.tmp
diff t1.tmp t2.tmp > t12.tmp

I have to this comparison for 100 files, so looking for a cleaner way without doing tr and storing in tmp file
# 2  
Old 08-07-2015
Hello Wahi,

Could you please try following and let me know if this helps you.
Code:
 awk -F, 'FNR==NR{split($0, A,",");next} {for(i=1;i<=NF;i++){D=$i;for(D in A){delete A[i]}}} END{for(j in A){print A[j]}}' t2 t1

output will be as follows.
Code:
e
f

EDIT: Adding a non one-liner form of solution too.
Code:
awk -F, 'FNR==NR        {
                                split($0, A,",");
                                next
                        }
                        {       for(i=1;i<=NF;i++)      {
                                                                D=$i;
                                                                for(D in A)     {
                                                                                        delete A[i]
                                                                                }
                                                        }
                        }
         END            {       for(j in A)             {
                                                                print A[j]
                                                        }
                        }
         ' t2 t1

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-07-2015 at 10:52 AM.. Reason: Added a non-one liner form of solution now
# 3  
Old 08-07-2015
Hi Ravinder,

Your solution almost got what I wanted. The only thing is that the code needs to check if the order is same too. I think I missed adding that in my original thread
e.g: t1.txt
Code:
a,b

t2.txt
Code:
b,a

It should tell that files are different and why

Maybe I should do a cksum first and then if different, check if it is different because of order, different because field name is different or extra field.
# 4  
Old 08-08-2015
So, what should be the resulting difference of this input:
Code:
a,b,c,d,e,f

and
Code:
b,c,d,e,f,a

?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two variables and print the difference

compare two variables and print the difference i have two variables X1=rac1,rac2 Y1=rac2,rac3 output=rac1,rac3 Use code tags to wrap code fragments or data samples. (1 Reply)
Discussion started by: jhonnyrip
1 Replies

2. Shell Programming and Scripting

Compare two variables and print the difference

Hi PRIM_SEQ=`some sql code` and output of PRIM_SEQ is like below 120 130 STB_SEQ=`some sql code` and output of STB_SEQ is like below 115 110 i need to compare this two variables output ( decimal numbers) 1) What I want to do is to compare every number in the PRIM_SEQ with... (8 Replies)
Discussion started by: amar1208
8 Replies

3. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

4. UNIX for Dummies Questions & Answers

Printing into two files under difference situation

I want to printing into two files under difference situation. For example, file 1 name.txt >gma-miR172a Glyma02g28845 >gma-miR1513a-3p Glyma02g15840 >gma-miR166a-5p Glyma02g15840 >gma-miR1530 Glyma02g15130 >gma-miR1507a Glyma02g01841 File 2 a.gff Glyma01g07930 ... (4 Replies)
Discussion started by: grace_shen
4 Replies

5. Shell Programming and Scripting

Compare two CSV files and put the difference in third file with line no,field no and diff value.

I am having two csv files i need to compare these files and the output file should have the information of the differences at the field level. For Example, File 1: A,B,C,D,E,F 1,2,3,4,5,6 File 2: A,C,B,D,E,F 1,2,4,5,5,6 out put file: (12 Replies)
Discussion started by: karingulanagara
12 Replies

6. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

7. Shell Programming and Scripting

Columns comparision of two large size files and printing the difference

Hi Experts, My requirement is to compare the second field/column in two files, if the second column is same in both the files then compare the first field. If the first is not matching then print the first and second fields of both the files. first file (a .txt) < 1210018971FF0000,... (6 Replies)
Discussion started by: krao
6 Replies

8. Shell Programming and Scripting

Comparing Columns and printing the difference from a particular file

Gurus, I have one file which is having multiple columns and also this file is not always contain the exact columns; sometimes it contains 5 columns or 12 columns. Now, I need to find the difference from that particular file. Here is the sample file: param1 | 10 | 20 | 30 | param2 | 10 |... (6 Replies)
Discussion started by: buzzusa
6 Replies

9. Shell Programming and Scripting

Compare difference

Hi every body Help required file 1 aaaaa bbbb cccccc dddd ffff File 2 aaaaa,1,ee,44,5t,6y, cccccc, ..... dddd, ..... eeeeee, ..... ffff, ...... ggg, ....... (7 Replies)
Discussion started by: The_Archer
7 Replies

10. Shell Programming and Scripting

to compare two files and to print the difference

suppose one file P1168S P2150L P85L Q597R R1097C Another file P2150L P85L Q597R R1097C R1379C R1587K Then output shud be R1379C R1587K thanks (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question