regex - Find and replace strings in file with $ selectors? -


i have file , i'm looking use () select piece of text, , $1 , $2 respectivley replace string.

http://127.0.0.1:5080/tarballs/(.+)/(.+).tgz  https://registry.npmjs.org/$1/-/$1-$2.tgz 

how can in unix? want run command this:

$ regex-replace ./file.txt "http://127.0.0.1:5080/tarballs/(.+)/(.+).tgz" "https://registry.npmjs.org/$1/-/$1-$2.tgz" 

you can use sed:

s='http://127.0.0.1:5080/tarballs/user/file.tgz'  sed -e 's~http://127\.0\.0\.1:5080/tarballs/(.+)/(.+.tgz)$~https://registry.npmjs.org/\1/-/\1-\2~' <<< "$s"  https://registry.npmjs.org/user/-/user-file.tgz 
  • dot special metacharacter in regex needs escaped, otherwise match character.
  • ~ used regex delimiter in s (substitute) command of sed.
  • -e used extended regex support.
  • \1 , \2 back-references of 2 captured groups.

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -