Visit The New, Modern Unix Linux Community


Compare 2 file and write result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare 2 file and write result
# 1  
Compare 2 file and write result

Hi friends...I have 2 files, file1.txt and reference.txt

I could able to find difference using
Code:
diff

and following command

Code:
awk 'NR == FNR { A[$0]=1; next } !A[$0]' reference.txt file1.txt

above command listing data which is not in reference.txt

Code:
12.12    87
11.95    88
11.83    89
12.55    84
12.42    85
12.27    86
11.82    89
12.55    84.03
12.38    85.15
12.1    87.03
11.97    88
12.25    86.03
12.07    87.42
11.9    88.35
11.8    89.07

here my intension is to list all data from file1.txt and print text "not available" to data which is not available in reference.txt

Code:
output looks like this
9    72.92         available
9.75    72.77    available
10.57    72.75  available
10.53    73       available
10.43    73.5    available
10.33    74       available
9.97    76         available
11.95    88       not available
11.83    89       not available
12.55    84       not available
12.42    85       not available
12.27    86       not available
11.82    89       not available

# 2  
Code:
awk 'NR==FNR{A[$0]=$0; next}{print A[$0]?A[$0] FS "available" : $0 FS "not available" }' reference.txt file1.txt

This User Gave Thanks to pamu For This Post:
# 3  
Code:
$ awk 'NR==FNR {Av[$0]++; next} {print $0 (!Av[$0]?" not":_) " available"}' reference.txt file1.txt

# 4  
Thanks pamu its working will you please explain..suppose files contains different number of columns say file1.txt contains 5 columns reference.txt contains 10 columns then
Code:
A[$0]=$0

, won't help I think
# 5  
Quote:
Originally Posted by Akshay Hegde
Thanks pamu its working will you please explain..suppose files contains different number of columns say file1.txt contains 5 columns reference.txt contains 10 columns then
Code:
A[$0]=$0

, won't help I think
Yes it won't work in that case. Because our matching criteria as $0 itself won't work here.

As per our matching pattern we can have arrays. Assume column 1 and 2 from file1.txt and column 2 and 5 from reference.txt. and print file1.txt.
Then we can use sth like this..

Code:
awk 'NR==FNR{A[$2,$5]=$0; next}{print A[$1,$2]?$0 FS "available" : $0 FS "not available" }' reference.txt file1.txt

Hope this helps you

pamu
This User Gave Thanks to pamu For This Post:
# 6  
Code:
awk     'NR==FNR {Av[++max]=$0; next}
                 {NT = "not "
                  for (i=1; i<=max; i++) if (Av[i] ~ $0) {NT=""; break}
                  print $0 "\t" NT "available"}
        '  reference.txt file1.txt

This works if the columns occur in the same order. If they don't, please! be way more specific!

Last edited by RudiC; 02-26-2013 at 03:02 AM.. Reason: typo
This User Gave Thanks to RudiC For This Post:
# 7  
Thank you RudiC and pamu both scripts are working fine.

---------- Post updated 02-27-13 at 12:16 AM ---------- Previous update was 02-26-13 at 02:10 AM ----------


here I want to include one more thing

I want to print unmatched reference file data also along with available and not available data in file1.txt

Code:
print A[$1,$2]?$0 FS "available" : $0 FS "not available"

How can I print it

I expect result like this

Code:
9            72.92          available 
9.75       72.77     available 
10.57     72.75    available 
10.53     73              available 
10.43     73.5        available
10.33     74             available 
9.97       76                available 
11.95     88              not available 
11.83     89             not available 
12.55    84              unmatched reference file 
12.42     85              unmatched reference file


Last edited by Akshay Hegde; 02-27-2013 at 01:43 AM..

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #407
Difficulty: Medium
Cygwin's default package selection is quite small, containing little more than the bash shell and the core file manipulation utilities expected of a Unix command line.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Curl and write custom result to file

I have the following script, for i in $(cat MyWebApps); do curl -u manager:tH1s1s4f3k3p4ssw0Rd http://10.10.10.10:7529/manager/jmxproxy/"?get=Catalina:type=Manager,context=/$i,host=localhost&att=activeSessions"; done Which gives me an output like this OK - Attribute get... (12 Replies)
Discussion started by: charli1
12 Replies

2. Shell Programming and Scripting

Compare 2 columns from the same file and print a value depending on the result

Hello Unix gurus, I have a file with this format (example values): label1 1 0 label2 1 0 label3 0.4 0.6 label4 0.5 0.5 label5 0.1 0.9 label6 0.9 0.1 in which: column 1 is a row label column 2 and 3 are values I would like to do a simple operation on this table and get the... (8 Replies)
Discussion started by: ksennin
8 Replies

3. Shell Programming and Scripting

Compare two and write updated output in third file

Hi Linux Experts. I have a requirement where i need to update the thousands of table definitions to extend the column length and character set therefore i am looking for some sort of linux script which i can use to update the length and chacterset. I have two files In first file i have 7... (1 Reply)
Discussion started by: Black-Linux
1 Replies

4. Shell Programming and Scripting

How to compare 2 files and create a result file with unmatched lines from first file.?

HI, I have 2 text files. file1 and file2. file1.txt (There are no duplicates in this file) 1234 3232 4343 3435 6564 6767 1213 file2.txt 1234,wq,wewe,qwqw 1234,as,dfdf,dfdf 4343,asas,sdds,dsds 6767,asas,fdfd,fdffd I need to search each number in file1.txt in file2.txt's 1st... (6 Replies)
Discussion started by: Little
6 Replies

5. Shell Programming and Scripting

How to compare the current result with previous line result.?

Hi Gurus, I have requirement to compare current result with previous reuslt. The sample case is below. 1 job1 1 1 job2 2 1 job3 3 2 job_a1 1 2 job_a2 2 2 job_a3 3 3 job_b1 1 3 job_b2 2 for above sample file, GID is group ID, for input line, the job run... (1 Reply)
Discussion started by: ken6503
1 Replies

6. Shell Programming and Scripting

Compare two files and write data to second file using awk

Hi Guys, I wanted to compare a delimited file and positional file, for a particular key files and if it matches then append the positional file with some data. Example: Delimited File -------------- Byer;Amy;NONE1;A5218257;E5218257 Byer;Amy;NONE1;A5218260;E5218260 Positional File... (3 Replies)
Discussion started by: Ajay Venkatesan
3 Replies

7. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

8. Shell Programming and Scripting

Script to write result to a file

Hello, How can I run this script every 1 hour and save its result to result.txt ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' Regards Shaan (5 Replies)
Discussion started by: Shaan_Shaan
5 Replies

9. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

10. Shell Programming and Scripting

Write result to output file

Good morning everybody, Beeing an absolute newbie in shell scripting I would like to look for some help here. I would like to read an external text file and format the data and write it to an output file. What I was trying to do was to display the result (this worked). But now I... (1 Reply)
Discussion started by: bluejean1976
1 Replies

Featured Tech Videos