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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern matching

Status
Not open for further replies.

inunix

Technical User
Jul 30, 2002
53
US
Hi Guys,
let me start with eg
<baba>
<juke>abc def</juke>
<abc></abc>
<ijk>789</ijk>
<ooo><ooo>
<love>luv</love>
</baba>
like this a file might contain 'n' number of <baba>..</baba> tags...and other tags also.
I need to find out this <baba> tag, then i need to check whether <abc> and <love> tag have values or not.. here in the above case abc doesn't but love has. <abc> and <love> tags present in few other tags like <baba>.
Basically i need to find files with these patterns.

Hope i'm not confused.

pls advice.

Thanks in advance
 
Something like this:
Code:
function fmatch(arrayp,str, mm) {

      for (mm in arrayp) {
          if (str ~ arrayp[mm]) { 
             split(str,arr,&quot;>&quot;) 
             sub(/<.*/,&quot;&quot;,arr[2])
             if (arr[2]) {
                print arr[2]
             }
          }
      }
}  
              
BEGIN {FS = &quot;>&quot; ; arraypats[1] = &quot;<abc>&quot; ; arraypats[2] = &quot;<love>&quot; ; arraypats[3] = &quot;<juke>&quot; }

{
   for (x=1 ; x < NF ; x++) {
        if ($x == &quot;<baba&quot;) {
           while ($x != &quot;</baba&quot;) {
                 fmatch(arraypats,$0)
                 getline
           } 
        }
   }
}

My output given your sample:
abc def
luv

How to use the data generated to search for
matching words in other files I leave as an
exercise for you.

 
Try....
awk 'BEGIN {a[1]=&quot;abc&quot;;a[2]=&quot;love&quot;}
/<baba>/,/<\/baba>/ {
for (i in a)
if ($0 ~ &quot;<\/&quot; a &quot;>&quot;)
print a &quot;=&quot; substr($0,length(a)+3,length-length(a)*2-5);
}' file1

Tested...
abc=
love=luv
 
Below is my code,(could be in-effecient way of programming).. but just thought it might help to understand my need clearly...
---------------------------------------------
BEGIN{
startflag=0
cbcfound=0
pgone=0
}

/\<dod_type\>TEST PGM\<\/dod_type\>/ { startflag=1 };
/\<temp\>99801\<\/temp\>/ { pgone=1 };
/\<\/image\>/
{
if (startflag == &quot;1&quot; && pgone = &quot;1&quot; )
{
if (cbcfound == &quot;4&quot; )
print &quot;All datas found for file &quot;, inpfile >> outrpt
else
print &quot;Few datas missing for file &quot;, FILENAME >> &quot;outrpt&quot;
}
startflag=0
cbcfound=0
pgone=0
};
/\<co_123\>.+\<\/co_123\>/
{
if (startflag == &quot;1&quot;)
{ cbcfound=1 }
};
/\<bat_113\>.+\<\/bat_113\>/
{
if (startflag == &quot;1&quot;)
{ cbcfound=cbcfound+1 }
};
/\<ten_a_ball\>.+\<\/ten_a_ball\>/
{
if (startflag == &quot;1&quot;)
{ cbcfound=cbcfound+1 }
};
/\<test_it\>.+\<\/test_it\>/
{
if (startflag == &quot;1&quot;)
{ cbcfound=cbcfound+1 }
};
{}
END{ }
---------------------------------------------

Basically i need to get a list of files those have these 4 tags and those doesn't.
my output should be like...
file1 : all CO_bat_ten_test_tags present for TEST PGM
file2 : Any one of the tag missing for the TEST PGM

Thanks for all your help.
 
This is a trivial exercise for grep or egrep.
Just put your tags in an array and then loop
through the array members searching for files
that have them keeping count of matches. If your
count == 4 then process the file. You reset your
count at the end of each file search.

You don't need awk for this.
 
Thanks for your suggestion. marsd.
I'll try.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top