我試圖用我的任何 url 的自定義值替換查詢字串。但它沒有正確決議它。
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
但我想要這樣的結果
http://sub.domain.com/file.jsp;jsessionid=SOMETHING?one=SOMETHING&two=SOMETHING
有什么方法可以使用sed或awk
uj5u.com熱心網友回復:
嘗試這個:
#!/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#'
結果:
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
我使用的方法是保留你想要的一切,并用你想要的值替換你不想要的。因此,將 SOMETHING1-2-3 替換為您想要使用的任何內容。
恕我直言,它確實使它更長但更容易理解。
uj5u.com熱心網友回復:
使用 sed
$ sed 's/=[^?\|&]*/=SOMETHING/g' input_file
http://sub.domain.com/file.jsp;jsessionid=SOMETHING?one=SOMETHING&two=SOMETHING
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/368373.html
上一篇:測驗從鍵盤接收輸入的程式
