Sed recognizing only a portion of search pattern

Viewed 54

I'm looking for a way to replace all the occurrences of a website in a file with a constant. I'm using gsed and regex on my mac (don't get deviated with the term mac, as this is the same output I'm getting even when executing on a windows machine) to accomplish this. I'm able to successfully validate the regex on regex101.com but sed substitution is failing for some reason

gsed --version : gsed (GNU sed) 4.8

(g)sed command:

find . -type f -path "./file1.txt"  -exec gsed -i -E -f /tmp/scripts/regex {} \;

/tmp/scripts/regex contents:

s/(ftp|http[s]?):\/\/([\w\.-]+)/\1{Your_Site}/gI

Sample file1.txt contents:

* "{\n \"firstName\": \"\",\n \"lastName\": \"\",\n \"street1\": \"\",\n \"street2\": \"\",\n \"city\": \"\",\n \"state\": \"\",\n \"postalCode\": \"\",\n \"country\": \"\",\n \"domain\": \"http://example.org\",\n \"action\": \"addUser\",\n \"token\": \"\",\n \"transId\": \"1413290890.usr.209883490\",\n \"customerId\": \"145qjk345kl_908jkl.345\",\n  \"src_name\": \"Your_Application\",\n \"channel\": \"webpage\",\n \"accountId\": \"0097892hjke6987hiuw.ACNT.hsapou8972rjk\",\n \"system\": \"Your_System\",\n \"originatingSystem_code\": \"Your_System_Id\",\n \"purchase_currency\": \"USD\",\n \"url\": \"https://another-link-to-my-example.org/add-user/new\",\n \"createFlag\": \"on\",\n \"web_version\": \"7\",\n

Expected output:

* "{\n \"firstName\": \"\",\n \"lastName\": \"\",\n \"street1\": \"\",\n \"street2\": \"\",\n \"city\": \"\",\n \"state\": \"\",\n \"postalCode\": \"\",\n \"country\": \"\",\n \"domain\": \"http://{Your_Site}\",\n \"action\": \"addUser\",\n \"token\": \"\",\n \"transId\": \"1413290890.usr.209883490\",\n \"customerId\": \"145qjk345kl_908jkl.345\",\n  \"src_name\": \"Your_Application\",\n \"channel\": \"webpage\",\n \"accountId\": \"0097892hjke6987hiuw.ACNT.hsapou8972rjk\",\n \"system\": \"Your_System\",\n \"originatingSystem_code\": \"Your_System_Id\",\n \"purchase_currency\": \"USD\",\n \"url\": \"https://{Your_Site}/add-user/new\",\n \"createFlag\": \"on\",\n \"web_version\": \"7\",\n

Current output:

* "{\n \"firstName\": \"\",\n \"lastName\": \"\",\n \"street1\": \"\",\n \"street2\": \"\",\n \"city\": \"\",\n \"state\": \"\",\n \"postalCode\": \"\",\n \"country\": \"\",\n \"domain\": \"http://{Your_Site}xample.org\",\n \"action\": \"addUser\",\n \"token\": \"\",\n \"transId\": \"1413290890.usr.209883490\",\n \"customerId\": \"145qjk345kl_908jkl.345\",\n  \"src_name\": \"Your_Application\",\n \"channel\": \"webpage\",\n \"accountId\": \"0097892hjke6987hiuw.ACNT.hsapou8972rjk\",\n \"system\": \"Your_System\",\n \"originatingSystem_code\": \"Your_System_Id\",\n \"purchase_currency\": \"USD\",\n \"url\": \"https://another-link-to-my-example.org/add-user/new\",\n \"createFlag\": \"on\",\n \"web_version\": \"7\",\n

Please ask for any additional info if I might have missed.

1 Answers

Use [:alnum:] character class or similar instead of \w inside bracket expressions.

I've highlighted the change below:

s/(ftp|http[s]?):\/\/([\w\.-]+)/\1{Your_Site}/gI
                       ^^

s/(ftp|http[s]?):\/\/([[:alnum:]\.-]+)/\1{Your_Site}/gI
                       ^^^^^^^^^

Please note that this RE is still too permissive and will match invalid names, in case that is of concern.

Related