我正在寻找一种方法,用常量替换文件中出现的所有网站。我在mac上使用gsed
和regex(不要偏离术语mac,因为这与我在windows机器上执行时得到的输出相同(来实现这一点。我能够在regex101.com上成功验证regex,但由于某种原因,sed替换失败了
gsed --version : gsed (GNU sed) 4.8
(g)sed
命令:
find . -type f -path "./file1.txt" -exec gsed -i -E -f /tmp/scripts/regex {} ;
/tmp/scripts/regex
内容:
s/(ftp|http[s]?)://([w.-]+)/1{Your_Site}/gI
样品file1.txt
内容:
* "{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
预期输出:
* "{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
电流输出:
* "{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
如果我可能错过了,请询问更多信息。
在括号表达式中使用[:alnum:]
字符类或类似字符,而不是w
。
我在下面强调了变化:
s/(ftp|http[s]?)://([w.-]+)/1{Your_Site}/gI
^^
s/(ftp|http[s]?)://([[:alnum:].-]+)/1{Your_Site}/gI
^^^^^^^^^
请注意,此RE仍然过于宽松,并且将匹配无效名称,以防引起关注。