Home > database >  Sed wont replace variable created inside for loop
Sed wont replace variable created inside for loop

Time:01-18

I'm trying to replace a value with sed in bash but when I am trying to use a variable which I have created inside the for loop it gives me the error: sed: -e expression #1, char 12: unknown option to s However, when I tried with creating a variable outside of the for loop, it works fine which botters me what I am actually doing wrong.

The following code works

#!/bin/bash
testvar="test"

for url in $(cat SpideredFinish.txt); do
    subdomain=$url  
    RuleForHTTP1=$(echo $subdomain | cut -f3 -d "/" | sed -e 's/^/https:\/\//')
    echo $url | xargs -I '%' sh -c "curl -k -s \"%\" | sed \"s/[;}\)>]/\n/g\" | grep -Po \"(['\\\"](https?:)?[/]{1,2}[^'\\\"> ]{5,})|(\.(get|post|ajax|load)\s*\(\s*['\\\"](https?:)?[/]{1,2}[^'\\\"> ]{5,})\"" | awk -F "['\"]" '{print $2}' | sort -fu | grep -E '^(/)' | sed "s/^/$testvar/"    
done

Now when I am making the change to use the variable RuleForHTTP1 it gives me the error shown above

The following code does NOT work

#!/bin/bash
testvar="test"

for url in $(cat SpideredFinish.txt); do
    subdomain=$url  
    RuleForHTTP1=$(echo $subdomain | cut -f3 -d "/" | sed -e 's/^/https:\/\//')
    echo $url | xargs -I '%' sh -c "curl -k -s \"%\" | sed \"s/[;}\)>]/\n/g\" | grep -Po \"(['\\\"](https?:)?[/]{1,2}[^'\\\"> ]{5,})|(\.(get|post|ajax|load)\s*\(\s*['\\\"](https?:)?[/]{1,2}[^'\\\"> ]{5,})\"" | awk -F "['\"]" '{print $2}' | sort -fu | grep -E '^(/)' | sed "s/^/$RuleForHTTP1/"
done

So what I need help with is to understand if there is any reason I can use my variable which I created inside the for loop which is RuleForHTTP1 to use as a replacement in my sed statement

Thank you!

CodePudding user response:

Your problem is that RuleForHTTP1 has forward slashes in it. So you cannot use the forward slash as a delimiter. Use a pipe:

sed "s|^|${RuleForHTTP1}|"
  • Related