Operating System - HP-UX
1763081 Members
3913 Online
108909 Solutions
New Discussion юеВ

Re: Search for a match in file2 (xml)

 
Chris Frangandonis
Regular Advisor

Search for a match in file2 (xml)


Hi All,

I am having a problem searching for a match from file2 (text) in file1 (xml), If found print line from file2 and append a "YES" at the end or else "NO"

I do get the results I want but that's if I remove the "".

Script
gawk -v opt="|]' 'BEGIN {
#RS = ""; FS = "";OFS = ","
}

NR == FNR {
b[$1] = $0
next
}

{
e = ""
for (x in b) {
if (match($0,x)) {
e = "YESSSSSSSSSSSS"
}
}
print $0" "e
}' File1 File2

File1
=====
^M

630642266
2010040202000000
0^M
IN


610354966
2010040202000000
7018
IN


630625666
2010040202000000
279
IN


File2
=====

630642266 913308 V 505848 1132175
640719766 915817 V 0 0
630625666 621524 V 165478144 348066498

Result should look like this

630642266 913308 V 505848 1132175 YES
640719766 915817 V 0 0 NO
630625666 621524 V 165478144 348066498 YES

Many Thanks
Chris
4 REPLIES 4
Dennis Handly
Acclaimed Contributor

Re: Search for a match in file2 (xml)

>gawk -v opt="|]' '
BEGIN {
#RS = ""; FS = "";OFS = ","
}
NR == FNR {
b[$1] = $0
next
}

It appears you want to create the map by doing a getline in the BEGIN block. Then reset your delimiters for the second file.

Or pass in the first file as a parm and use getline to read only from that file.

http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1358924
Chris Frangandonis
Regular Advisor

Re: Search for a match in file2 (xml)

Hi Dennis,

Thanks,

My mistake is should read as follows

gawk 'BEGIN{
FS = "[><]"
}
FNR == NR{
b[$1]=$3;next
But still I cannot get expected results

Thanks
Chris
Jean-Yves Picard
Trusted Contributor

Re: Search for a match in file2 (xml)

Hello,

no too subtle, what about :

FILENAME == "file1.xml" && $1 ~ /^ split($0,A,">");
poid_tmp=substr(A[2],1,index(A[2],"<")) ;
poid[poid_tmp] = 1 ;
next ;
}

FILENAME == "file2.txt" {
if ( $1 in poid ) printf "%s YES\n",$0 ;
else printf "%s NO\n",$0 ;
}

whith appropriate name ?

Jean-Yves Picard
Dennis Handly
Acclaimed Contributor

Re: Search for a match in file2 (xml)

If your "POID" tokens are on a separate line you can do:
awk -v map=File1 '
BEGIN {
FS = "[><]"
while (getline < map > 0) {
if ($2 == "POID") {
# print "map:", $3
map_poid[$3] = $0
}
}
close(map)
FS = "[ \t]"
}
{
if (map_poid[$1] == "") result = "NO"
else result = "YES"
print $0, result
}' File2