Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extracting the difference between two files

Status
Not open for further replies.

agilo

Programmer
Feb 4, 2004
73
TR
Hi,

I have the following two files:
test1.txt:

syxe weq eww
cxa 322 342
wq1 234 edd
343 dss sds

and test2.txt

syxe weq eww
cxa 322 342
wq1 234 edd
341 dss sds
syx weq eww
cx2 322 342
wq3 234 edd
347 dss sds


and I used the following program to extract the lines which are in test2.txt and not in test1.txt:

awk '
BEGIN{
while((getline<"test2.txt")>0){
a[$1]=$1;b[$1]=$0
}
}
{
if ($1!=a[$1]) printf "%s\n",b[$1]
}
' test1.txt

Using this script, does not produce any output..

Can anybody help what is wrong in this script.

Thanks in advance

 
the lines which are in test2.txt and not in test1.txt
awk '
BEGIN{while((getline<"test1.txt")>0)++t[$1]}
{ if (!t[$1]) print }
' test2.txt


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Code:
NR==FNR { a[$0]++ ; next }
{ if ( !($0 in a) ) print }

If you want to ignore variations in whitespace:
Code:
{ $1=$1 }
NR==FNR { a[$0]++ ; next }
{ if ( !($0 in a) ) print }
 
I was a bit too verbose.
Code:
NR==FNR { a[$0]++; next }
!($0 in a)
If you want to ignore variations in whitespace:
Code:
{ $1=$1 }
NR==FNR { a[$0]++; next }
!($0 in a)
 
Thanks you Guys, these two solutions solved the problem.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top