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

Hi all, I want to check out whet

Status
Not open for further replies.

urug

Technical User
Sep 18, 2003
2
US
Hi all,

I want to check out whether a line contains predefined keywords or not. The presence of a keyword is marked by 1, the non-presence by 0. I give you an example:

I'm eating pizza with salami.
Pizza is Italian food.
Pizza with salami is good food.

For this example I need a script that searches for the keywords (in this order!) 'pizza', 'salami' and 'food'. On the first line AWK will find the first two keywords. Keyword 'food', however, is not present in this context. Therefore, the output should be '1 1 0'. For the second line, it should be '1 0 1' and for the last line it should be '1 1 1' ('cause all three keywords are present!).

The values that AWK returns, should be saved to another file. This file (e.g. keywords.txt) should eventually look like this:

1 1 0
1 0 1
1 1 1

I hope someone can help me with this! Thanks.

 
Does this mean that the phrase "Food is Pizza" should return 0 0 0 ?

Greg.
 
predefined keywords should be in a file called predef.txt
awk script is sea.sc

BEGIN {
while ((getline kw_line < &quot;predef.txt&quot;) > 0) kw[++i]=tolower(kw_line)
}
{
for (j=1;j<=i;j++ ) printf (&quot;%d &quot;,(index(tolower($0),kw[j]) > 0 ? 1 : 0))
printf (&quot;\n&quot;)
}

run from prompt
awk -f sea.sc INPUTFILE > keywords.txt
 
Thanks, daddymack. Your script works perfectly well!

Greetings,

Urug
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top