Arun Arun - 3 years ago 115
Bash Question

Bash Issue : sed: -e expression #1, char 16: unknown option to `s'

I'm trying pass a

request and From response body , Im trying to grep for a key word "coin". If "
" word matches , I need to get either "0" or "1" with the input URL as a
( comma

$for i in $(cat small);do curl -i -H "Accept: application/json" $i | grep "coin";echo $? | sed "s/^\(.\)/$i,\1/";done

#cat small

I'm adding input as a prefix to the result "sed "s/^(.)/$i,\1/".

But i'm getting error as

$ for i in $(cat small);do curl -i -H "Accept: application/json" $i | grep "coin";echo $? | sed "s/^\(.\)/$i,\1/";done
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 633k 0 633k 0 0 909k 0 --:--:-- --:--:-- --:--:-- 908k
<script src=""></script>
sed: -e expression #1, char 16: unknown option to `s'

Any suggestions on how to solve this ? Though the above is not a good practice , But it will help me timely.

Expected Result :,0

Answer Source

I think a right way of coding this in bash avoiding useless cat usages and right usage of the tools (grep, return code) would be doing

#!/usr/local/env bash

while IFS= read -r url; do
    if curl -i -H "Accept: application/json" "$url" 2>&1 | grep -q "coin"; then
        printf "%s,0\n" "$url" 
        printf "%s,1\n" "$url" 
done < urlFile

The above code uses right way to

  1. Loop through a file using input file re-direction and using a while loop and the read command (avoids breaking up lines when it has spaces)
  2. Using return code of grep directly in if clause by suppressing the search output -q to console
  3. Passing both stdout and stderr of curl to grep to look for the keyword.
  4. Double-quoting the variables to not let them split due to Word splitting when spaces are present.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download