benhowdle89
benhowdle89

Reputation: 37454

Search curl output for string with Bash

Trying to fetch a webpage as a lowercase string, then search the string output for a substring

My attempt:

  1 #!/usr/bin/env bash
  2
  3 URL="https://somesite.com"
  4 MOVIES_SOURCE="movies.txt"
  5 PAGE=`curl "$URL"` | tr '[:upper:]' '[:lower:]'
  6
  7 while IFS= read -r movie
  8 do
  9   FOUND="$($PAGE =~ "$movie")"
 10   echo $FOUND
 11   if [[ $FOUND ]]; then
 12    echo "$movie found"
 13   fi
 14 done < $MOVIES_SOURCE
 15

When I run this, I'm receiving line 9: =~: command not found

The $movie variable is valid and contains each line from movies.txt, but I'm struggling to figure out this one!

Upvotes: 1

Views: 2043

Answers (1)

Chris Maes
Chris Maes

Reputation: 37712

If you want to use regex matching in bash:

if [[ $PAGE =~ $movie ]]; then
    echo "$movie found"
fi

example:

PAGE="text blah Avengers more text"
movie="Avengers"
if [[ $PAGE =~ $movie ]]; then
    echo "$movie found"
fi

gives:

Avengers found

Also: to capture the output of the whole curl command:

PAGE=$(curl "$URL" | tr '[:upper:]' '[:lower:]')
  • always prefer $() over backticks
  • you had to wrap your whole command in order for $PAGE to contain the output where you converted to lowercase.

Upvotes: 1

Related Questions