Visit Our UNIX and Linux User Community


diff of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting diff of files
# 1  
Old 09-15-2009
diff of files

Hi,

I have 2 files.I want to check if file1 is contained in file2.

A.txt:
-----
AAA
BBB

B.txt:
------
CCC
AAA
BBB
DDD

I want to check if A.txt is contained in B.txt. Can it be done using SED ?
# 2  
Old 09-15-2009
Giri,

Perhaps this post might help you.


Regards,

Praveen
# 3  
Old 09-15-2009
No...That was too complicated.

I know it can be done easily using sed.I jus need to print pass/fail if file1 is contained/not contained in file2.
# 4  
Old 09-15-2009
I hope this helps you...
Code:
 
awk 'NR==FNR{a[$1]=1;next}{if(a[$1])print $1,"Pass"
 else print $1,"Fail"}' B.txt A.txt

# 5  
Old 09-16-2009
Hi malcomex999 ,

I tried the code , but it gives output as

CCC Fail
AAA Fail
BBB Fail
DDD Fail
AAA Fail
BBB Fail
# 6  
Old 09-16-2009
This is what i get, can you make sure that what is in your file is same as what you posted in here.
Code:
 
>cat A.txt
AAA
BBB
EEE
>cat B.txt
CCC
AAA
BBB
DDD
>awk 'NR==FNR{a[$1]=1;next}{if(a[$1])print $1,"Pass"
>else print $1,"Fail"}' B.txt A.txt
AAA Pass
BBB Pass
EEE Fail

# 7  
Old 09-16-2009
Try this awk script:


Code:
(NR == FNR){
 a[$1]=0
 next
}

{
if ($1 in a)
 print $1, " pass" 
else 
print $1, " fail"
}

Usage:

Code:
awk -f <name_of file_with_above_code> <name_of_a_file> <name_of_b_file>

Don't put the <> in the command line...
Note: I generally chose to put the key file (a.txt) in arrary rather than the data file (b.txt) because the first is usually shorter. I also use the key in array test in the if statement because I get to skip the check for value bit. Most of this doesn't really matter until b.txt gets really big.

Last edited by jp2542a; 09-16-2009 at 07:05 AM.. Reason: missing stuff

Previous Thread | Next Thread
Test Your Knowledge in Computers #610
Difficulty: Medium
Compared to Python, Perl has a relatively small Comprehensive Perl Archive Network (CPAN) library.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Diff 3 files, but diff only their 2nd column

Guys i have 3 files, but i want to compare and diff only the 2nd column path=`/home/whois/doms` for i in `cat domain.tx` do whois $i| sed -n '/Registry Registrant ID:/,/Registrant Email:/p' > $path/$i.registrant whois $i| sed -n '/Registry Admin ID:/,/Admin Email:/p' > $path/$i.admin... (10 Replies)
Discussion started by: kenshinhimura
10 Replies

2. Shell Programming and Scripting

Diff 2 Delimiter Files.

Hi, I need a small help to compare two PIPE delimited files. Note : All The Files Have A Column Name Example: file1.txt ================= column1|column2|column3 one|two|three four|five|six seven|eight|nine file2.txt ================= column1|column2|column3 1|two|three... (2 Replies)
Discussion started by: anshultongia
2 Replies

3. Shell Programming and Scripting

.procmailrc and uudeview (put attachments from diff senders to diff folders)

Moderator, please, delete this topic (1 Reply)
Discussion started by: optik77
1 Replies

4. Shell Programming and Scripting

Using Diff to Compare 2 files

Hi I've been trying various methods that I have found online with regards to comparing 2 files using the diff command. Nothing seems to work. The problem is that I'm not too familiar with the proper syntax. Can you please assist me. Here is my script: #!/bin/bash awk -F',' -v file1="$1"... (9 Replies)
Discussion started by: ladyAnne
9 Replies

5. Shell Programming and Scripting

diff bw two files

Hi All, I have two files which look as below File1 serial="1" name="abc" type="employee" field="IT" serial="2" name="cde" type="intern" field="Marketing" serial="3" name="pqr" type="contractor" field="IT" serial="4" name="xyz" type="employee" field="Sales" File2 serial="1"... (3 Replies)
Discussion started by: grajp002
3 Replies

6. UNIX for Dummies Questions & Answers

Diff command of two files

Hi, I use the diff command to compare two files and append this output to a file. I would like to now not only produce the differences but be able to output the total number of changes made, the number of new files added and the number of files deleted, is there I can do this using the diff... (2 Replies)
Discussion started by: cyberfrog
2 Replies

7. Shell Programming and Scripting

Diff b/w 2 files

Hi Masters, I have two files named file1 and file2. Both the files contains the same contents with some difference in comments,space.But no content change. I tried to find the diff between the two files to make sure that contents are same. For that i tried diff -ibw file1 file2 But... (1 Reply)
Discussion started by: ecearund
1 Replies

8. Shell Programming and Scripting

Find duplicates from multuple files with 2 diff types of files

I need to compare 2 diff type of files and find out the duplicate after comparing each types of files: Type 1 file name is like: file1.abc (the extension abc could any 3 characters but I can narrow it down or hardcode for 10/15 combinations). The other file is file1.bcd01abc (the extension... (2 Replies)
Discussion started by: ricky007
2 Replies

9. UNIX for Dummies Questions & Answers

get diff beteween two files

I have file1 and file2. I am trying to get the difference between the two of them to another file? I have tried diff and fgrep with no luck.. I dont think I am using the proper switches to make this happen. Can someone please give me the key to this lock? Muchos Thanks. Tbone (2 Replies)
Discussion started by: jtrevinojr
2 Replies

10. Shell Programming and Scripting

diff 2 files; output diff's to 3rd file

Hello, I want to compare two files. All records in file 2 that are not in file 1 should be output to file 3. For example: file 1 123 1234 123456 file 2 123 2345 23456 file 3 should have 2345 23456 I have looked at diff, bdiff, cmp, comm, diff3 without any luck! (2 Replies)
Discussion started by: blt123
2 Replies

Featured Tech Videos