Compare 2 file and write result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare 2 file and write result
# 8  
Old 02-27-2013
Are there duplicate lines in file1?
If not, you can delete Av[i] just before the break, and in an END section print out the remaining Av entries.
If yes, you need a second array indexed like Av to hold the fact of a match, and then, in the END section, print out the Av elements that do not have a "match" entry.
# 9  
Old 02-27-2013
yes, there are some records in file1.txt, which are same as that of reference file.
script works fine as per my requirement, but for some other purpose I want to print unmatched records in reference file also along with file1.txt

Thank you for your reply I will try.

---------- Post updated at 05:45 AM ---------- Previous update was at 02:56 AM ----------

Pamu will you please help me to implement #7 in your script

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

# 10  
Old 02-27-2013
Is this what you want....?

Code:
awk 'NR==FNR{A[$0]=$0; next}{print A[$0]?A[$0] FS "available" : $0 FS "not available";delete A[$0] }
    END{for (i in A){if(A[i]){print A[i],"unmatched reference file"}}}' reference.txt file1.txt

This User Gave Thanks to pamu For This Post:
# 11  
Old 02-27-2013
Yes, Thank you so much..
please explain pamu I will try to learn from you
# 12  
Old 02-27-2013
Quote:
Originally Posted by Akshay Hegde
Yes, Thank you so much..
please explain pamu I will try to learn from you
Code:
awk 'NR==FNR{A[$0]=$0; next}    # Here we read reference.txt file and store it in array A with index as $0.

{print A[$0]?A[$0] FS "available" : $0 FS "not available";    # Here we check $0 is available or not in Array A. If $0 is present then print as available or not available.

delete A[$0] }    # Here for those who are availabe delete those records from Array.

END{for (i in A){if(A[i]){print A[i],"unmatched reference file"}}}'    # Here print those records which is not present in file1.txt

Hope this helps you Smilie

pamu
This User Gave Thanks to pamu For This Post:
# 13  
Old 02-27-2013
Thanks a lot for nice explanation.
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question