1

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!

3
  • Use the ~ operator inside [[..]] Commented May 22, 2019 at 12:47
  • Adding the line numbers makes it more difficult to copy and test your code, by the way. Commented May 22, 2019 at 13:40
  • I'm a bit unclear about what you're trying to do...some sample input and corresponding desired output would make things clearer. I suspect that this can be reduced to a short line of awk. Commented May 22, 2019 at 14:07

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.