how to do like a grep in a sketch?

So I got my ethernet shield just now, and I’d like to try to do something based on what I read from a web page. If I were shell scripting, I’d grep for the word I’m interested in. How do I do this in a sketch?

To make my question less vague, I’ve started with the WebClient example sketch, which pulls a query from google. How can I find out if a certain word (say YES or NO) exists on the page?

Thanks!

Below is the example code without modification.

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte server[] = { 64, 233, 187, 99 }; // Google

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  
  delay(1000);
  
  Serial.println("connecting...");
  
  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

go through each character in the response,

if the character looks like the first character you are looking for, get the next character and compare it to the next character you are looking for, etc.

if you get to the end of the string that you are looking for then you found that word in the response.

Or,

Create a char array the same size as the word you are looking for to act as a temporary store.

Loop:
Get the next character from the response.
Shift each char in the temp array 1 left and drop the left most character.
Put the new character in the right most place in the array.
Compare the array to the word you are looking for. strcmp()
If they match youve found it.
If not continue looping.

This is uncompiled but cut and pasted and edited from something Im working on.

char word = “yourtext”; // text you are looking for
bool match = false;
char temp[strlen(word)-1]; // initialise temp store to match the size of the word
temp[strlen(word)] = 0; // add the end of string marker

while (client.available() && !match){

// move each element of temp char array left 1 place
for( unsigned int i = 1; i<=strlen(word); i++){
temp[i-1] = temp*;*

  • }*
  • // add next character to char array before last /0 marker*
  • temp[strlen(word)-1] = client.read();*
  • // have we found the tag?*
  • if ( strcmp( temp, word)==0 ){ match=true;};*
  • }*
    If you want to search again you will have to do another web request unless you are expecting it to be after your last search result as clent.read cannot go back to the beginning.
    Im working on an xml reader at the moment. This is from my work in progress. Ill eventually get round to finishing it and will post it on the forum.
    Gordon

Nice! Thanks dcb, that was the nudge in the right direction I needed.

I got a brute force method to work, later I might work on generalizing it some.

Gordon, I'll watch for your xml reader. That will be super handy for all kinds of things.

Nothing wrong with brute force. My solution needed to keep careful track of its location in the character stream for later use.

Ive managed to put some overflows in my arrays. So the code will need tweeking in the loops with the strlen stuff.

Gordon