Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sed wont replace variable created inside for loop

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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}|"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading