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 ?

Giri,

Perhaps this post might help you.

Regards,

Praveen

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.

I hope this helps you...

 
awk 'NR==FNR{a[$1]=1;next}{if(a[$1])print $1,"Pass"
 else print $1,"Fail"}' B.txt A.txt

Hi malcomex999 ,

I tried the code , but it gives output as

CCC Fail
AAA Fail
BBB Fail
DDD Fail
AAA Fail
BBB Fail

This is what i get, can you make sure that what is in your file is same as what you posted in here.

 
>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

Try this awk script:

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

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

Usage:

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.

@malcomex999

arun:/home/giri/validate 298>cat A.txt
AAA
BBB
EEE
arun:/home/giri/validate 299>cat B.txt
CCC
AAA
BBB
DDD

arun:/home/giri/validate 300>cat test
awk 'NR==FNR{a[$1]=1;next}{if(a[$1])print $1,"Pass"
else print $1,"Fail"}' B.txt A.txt

arun:/home/giri/validate 301>./test
CCC Fail
AAA Fail
BBB Fail
DDD Fail
AAA Fail
BBB Fail
EEE Fail

---------- Post updated at 07:11 AM ---------- Previous update was at 07:07 AM ----------

@jp2542a

arun:/home/giri/validate 311>cat -n test1
1 (NR == FNR){
2 a[$1]=0
3 next
4 }
5
6 {
7 if ($1 in a)
8 print $1, " pass"
9 else
10 print $1, " fail"
11 }
arun:/home/giri/validate 308>cat A.txt
AAA
BBB
EEE
arun:/home/giri/validate 309>cat B.txt
CCC
AAA
BBB
DDD
arun:/home/giri/validate 310>awk -f test1 A.txt B.txt
awk: syntax error near line 7
awk: illegal statement near line 7
awk: syntax error near line 9
awk: illegal statement near line 9

why do you need to put it in file, just try it directly from command line.

both same results...

I suspect you might have some invisible charcters in your files...
Both syntax look fine. Trying just typing them in instead of copy/paste..

Guys...i ve got a logic here..
A.txt
-----
AAA
BBB

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

script
------
cat A.txt | while read var1
do
echo $var1
grep $var1 B.txt | sed -n -e '1p' >> tmp
done
diff -w A.txt tmp

Now i want to print Pass/Fail based on the output from diff...how can i do it ?

 
#!/bin/ksh
while read line
do
if [ `grep ${line} B.txt` ]; then
echo ${line} Pass
else
echo ${line} Fail
fi
done < A.txt