$ cat urls.txt
http://example.com/test/test/test?apple=&bat=&cat=&dog=
https://test.com/test/test/test?aa=&bb=&cc=
http://target.com/test/test?hmm=
我想要像下面這樣的輸出????,我怎么能在 bash 中做到這一點(單行命令)
$ cat urls.txt
http://example.com/test/test/test?apple=
http://example.com/test/test/test?bat=
http://example.com/test/test/test?cat=
http://example.com/test/test/test?dog=
https://test.com/test/test/test?aa=
https://test.com/test/test/test?bb=
https://test.com/test/test/test?cc=
http://target.com/test/test?hmm=
uj5u.com熱心網友回復:
使用 GNU awk:
$ awk -F'?|=&|=' '{for(i=2;i<NF;i ) print $1 "?" $i "="}' urls.txt
http://example.com/test/test/test?apple=
http://example.com/test/test/test?bat=
http://example.com/test/test/test?cat=
http://example.com/test/test/test?dog=
https://test.com/test/test/test?aa=
https://test.com/test/test/test?bb=
https://test.com/test/test/test?cc=
http://target.com/test/test?hmm=
uj5u.com熱心網友回復:
我嘗試使用 sed 但它很復雜。如果像這樣使用 perl:
perl -pe 'if(/(.*\?)/){$url=$1;s#&#\n$url#g;}' url.txt
它運作良好。
uj5u.com熱心網友回復:
awk使用GNU gensub():
awk '{print gensub(/^(https?:)(.*)(\?[[:alpha:]] =)(.*)/,"\\1\\2\\3","g")}' file
http://example.com/test/test/test?apple=
https://test.com/test/test/test?aa=
http://target.com/test/test?hmm=
gensub()用于在替換文本中指定正則運算式的組件,使用正則運算式中的括號來標記組件(此處為四個)。我們只列印其中的 3 個:"\\1\\2\\3".
uj5u.com熱心網友回復:
這可能對您有用(GNU sed):
sed -E 's/(([^?] \?)[^=] =)&/\1\n\2/;P;D' file
&用換行符和第一個引數之前的子字串替換每個,列印/洗掉第一行并重復。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/441517.html
