I have a file containing URLs with unique parameters, with some URLs having multiple parameters. I am trying to replace all characters after the = to something else.

Example URL in file-containing-urls.txt:


Example sed command:

sed -r '/^\=/!s/=.*$/=somevalue/g' file-containing-urls.txt

Though this works, it's only replacing the first parameter and nothing else. I would like all characters after an = to be a specific string. Here is my result:

https://somewebsite.com/somepath/?param=mystring  --> Works
https://somewebsite.com/new/path/?param1=mystring&param2=removed  --> Only works for param1

The second example, I would like my string to also carry over to param2, but it looks like sed is only replacing the first value and ignoring the rest. All other parameters are getting removed from the view. I thought a /g would fix this, but I'm always getting the same result. Thank you!

  • .*$ greedily consumes everything to the end of the line, so there's nothing left for /g to repeat Apr 25 at 19:26
$ sed 's/=[^=&]*/=somevalue/g' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.