bash - awk : how to pass and use array value -


i have awk script read search keywords input1.txt , search if search string present in input2.xml

$ cat myawk.awk nr==fnr { keywordarray[nr]=$0; next; }  /<record / { i=1 } { a[i++]=$0 } /<\/record>/ {     if (found) {         (i=1; i<=length(a); ++i) print a[i] >> result.txt     }     i=0;     found=0 } /<keyword>keyword1<\/keyword>/ { found=1 } /<keyword>keyword2<\/keyword>/ { found=1 } ..... 

this need help. need pass keyword value stored in keywordarray.

$ cat input1.txt keyword1 keyword2 keyword3 ...  $ cat input2.xml <record category="xyz"> <person ssn="" e-i="e"> <title xsi:nil="true"/> <position xsi:nil="true"/> <names> <first_name/> <last_name></last_name> <aliases> <alias>cdp</alias> </aliases> <keywords> <keyword xsi:nil="true"/> <keyword>keyword1</keyword> </keywords> <external_sources> <uri>http://www.google.com</uri> <detail>keyword1 present in abc xyz reason</detail> </external_sources> </details> </record>  <record category="abc"> <person ssn="" e-i="f"> <title xsi:nil="true"/> <position xsi:nil="true"/> <names> <first_name/> <last_name></last_name> <aliases> <alias>cdp</alias> </aliases> <keywords> <keyword xsi:nil="true"/> <keyword>dontsearch</keyword> </keywords> <external_sources> <uri>http://www.google.com</uri> <detail>search not present in abc xyz reason</detail> </external_sources> </details> </record>  $ awk -f myawk.awk input1.txt input2.xml 

you can use awk:

awk 'nr==fnr{keywords[$0]++;next} /^<record/{p=1;data=""} p{data = data rs $0;} /^<\/record/{for(key in keywords){if(data ~ key){print data}}}' input1.txt input2.xml 

more readable awk:

awk 'nr==fnr{keywords[$0]++;next}      /^<record/{p=1;data=""}       p{data = data rs $0;}       /^<\/record/{for(key in keywords){if(data ~ key){print data}}}' input1.txt input2.xml 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -