[英]How to use "sed" or "awk" to replace url parameters query string
I was trying to replace the query string with my custom value of any url.我试图用任何 url 的自定义值替换查询字符串。 But it is not parsing it correctly.但它没有正确解析它。
echo "http://sub.domain.com/file.jsp;jsessionid=2e62dbe69850d25bc7c6424ba59db?one=15&two=16" | sed -e "s!=[^&]*!=SOMETHING!g"
http://sub.domain.com/file.jsp;jsessionid=SOMETHING&two=SOMETHING
but I want result like this但我想要这样的结果
http://sub.domain.com/file.jsp;jsessionid=SOMETHING?one=SOMETHING&two=SOMETHING
Is there any way to do it using sed
or awk
有什么方法可以使用sed
或awk
Try this:尝试这个:
#!/bin/bash
url="http://sub.domain.com/file.jsp;jsessionid=2e62dbe69850d25bc7c6424ba59db?one=15&two=16"
echo "ORIGINAL: $url"
echo "NEW: $url" | sed -e 's#\(.*jsessionid=\).*\(?one=\).*\(&two=\).*$#\1SOMETHING1\2SOMETHING2\3SOMETHING3#'
Result:结果:
ORIGINAL: http://sub.domain.com/file.jsp;jsessionid=2e62dbe69850d25bc7c6424ba59db?one=15&two=16
NEW: http://sub.domain.com/file.jsp;jsessionid=SOMETHING1?one=SOMETHING2&two=SOMETHING3
The method I used is to keep everything you want, and replacing what you do not want with the values you desire.我使用的方法是保留你想要的一切,并用你想要的值替换你不想要的东西。 So replace SOMETHING1-2-3 with whatever you want to use.因此,将 SOMETHING1-2-3 替换为您想要使用的任何内容。
It does make it longer but simpler to understand, IMHO.恕我直言,它确实使它更长但更易于理解。
Using sed
使用sed
$ sed 's/=[^?\|&]*/=SOMETHING/g' input_file
http://sub.domain.com/file.jsp;jsessionid=SOMETHING?one=SOMETHING&two=SOMETHING
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.