Text-Processing

在一行中找到匹配的 URL,然後將它們縮短為域名

  • November 18, 2018

我有一個正在為其編寫腳本的日誌文件,因此它只顯示某些輸出欄位。我需要的最後一點是縮短 URL,以便在他們點擊“.com”、“.edu”、“.org”等時停止該行。有沒有辦法用 grep 做到這一點?我應該尋找其他命令嗎?

範例輸出為:

student1234 "GET https://www.noname.com:443/login"
student4567 "GET http:// www.noip.edu:80/start/noname"
student8901 "GET http:// www.testing.org:80/search/change"

我需要的是:

student1234 "GET https://www.noname.com
student4567 "GET http:// www.noip.edu
student8901 "GET http:// www.testing.org

這麼多選擇,選一個你喜歡的吧。

使用grep

grep -o '^[^:]\+:[^:]\+' file.txt

使用cut

cut -d: -f1-2 file.txt

使用awk

awk -F: '{ print $1$2 }' file.txt

使用sed

sed 's/^\([^:]\+:[^:]\+\).*/\1/' file.txt

使用外殼:

while IFS=: read -r i j k; do echo "$i$j"; done <file.txt

使用perl

perl -pe 's/^([^:]+:[^:]+).*/$1/' file.txt

例子:

$ grep -o '^[^:]\+:[^:]\+' file.txt
student1234 "GET https://www.noname.com
student4567 "GET http:// www.noip.edu
student8901 "GET http:// www.testing.org

$ cut -d: -f1-2 file.txt                                                
student1234 "GET https://www.noname.com
student4567 "GET http:// www.noip.edu
student8901 "GET http:// www.testing.org

$ awk -F: '{ print $1$2 }' file.txt                 
student1234 "GET https//www.noname.com
student4567 "GET http// www.noip.edu
student8901 "GET http// www.testing.org

$ sed 's/^\([^:]\+:[^:]\+\).*/\1/' file.txt            
student1234 "GET https://www.noname.com
student4567 "GET http:// www.noip.edu
student8901 "GET http:// www.testing.org

$ while IFS=: read -r i j k; do echo "$i$j"; done <file.txt
student1234 "GET https//www.noname.com
student4567 "GET http// www.noip.edu
student8901 "GET http// www.testing.org

$ perl -pe 's/^([^:]+:[^:]+).*/$1/' file.txt
student1234 "GET https://www.noname.com
student4567 "GET http:// www.noip.edu
student8901 "GET http:// www.testing.org

引用自:https://unix.stackexchange.com/questions/283650