Skip to content
Advertisement

Curl/sed command not processing inputs correctly

I’m having some trouble with getting this to do what I want it to do.

read -p "URL to read: " U

read -p "Word to fin: " O

read -p "Filename: " F

curl -O $U | sed "s/<$O>/*$O*/g" > $F.txt

So basically what I want is to use curl to get a .txt file from a url, then sort through it to find the word specified by the user input. Then mark all those words with a * and put them in a file specified by the user. Almost the exact same code works in Linux, but this doesn’t work on my Mac. Anyone got an idea?

Advertisement

Answer

Two issues:

  • -O makes curl store the downloaded file, not output it on stdout.
  • word boundary metacharacters < and > are a GNU extension. On BSD sed, you can use [[:<:]] and [[:>:]] instead.

This should work on OSX:

curl "$U" | sed "s/[[:<:]]$O[[:>:]]/*$O*/g" > $F.txt
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement