0

Consider a list of string/URL in a file as shown below

$ cat urls.txt
https://example.com/index.php?param1=value1&param2=value2&param3=value3
https://example2.com/home.php?param1=value1&param2=value2

I need to remove the parameter values as shown below

$ cat replaced.txt
https://example.com/index.php?param1=&param2=&param3=
https://example2.com/home.php?param1=&param2=

How can I acheive this? I have tried several variations using sed that ended up replacing everything between = and & as below

$ sed -r 's/(=)(.*)(&)/\1\3/g' urls.txt
https://example.com/index.php?param1=&param3=value3
https://example2.com/home.php?param1=&param2=value2

Thanks

1

try with

sed -r 's/(=)([^=]*)(&)/\1\3/g;s/(=)([^=]*)$/\1/'

where

  • s/(=)([^=]*)(&)/\1\3/g perform substitution on firsts param=value pattern, but stop on = (to avoid greedy match)
  • s/(=)([^=]*)$/\1/ substitute last pattern
| improve this answer | |
  • perfect. thanks for the solution and explanation. – Goron Sep 2 at 14:26
0

Tried with below command

sed  -e "s/[a-z]*[0-9]\&/\&/g" -e "s/[a-z]*[0-9]$//g" filename

output

https://example.com/index.php?param1=&param2=&param3=
https://example2.com/home.php?param1=&param2
| improve this answer | |

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.