從文字中取得 URL

從文字中取得 URL

我通過 獲得一個文本文件apt-get --print-uris dist-upgrade > /mnt/URIs.txt,我想下載帶有文本文件提供的 URL 的所有包,只有 '' 之間的文本是 URL,如何刪除其餘部分,因為只有 URL 和返回符號用於通過互聯網瀏覽器下載。

答案1

可能的輸出apt-get --print-uris dist-upgrade看起來像這樣:

Reading package lists...
Building dependency tree...
Reading state information...
Calculating upgrade...
The following packages will be upgraded:
  evolution-data-server evolution-data-server-common gir1.2-goa-1.0
  gnome-online-accounts libcamel-1.2-62 libebackend-1.2-10 libebook-1.2-20
  libebook-contacts-1.2-3 libecal-2.0-1 libedata-book-1.2-26
  libedata-cal-2.0-1 libedataserver-1.2-24 libedataserverui-1.2-2
  libgoa-1.0-0b libgoa-1.0-common libgoa-backend-1.0-1 libyelp0 linux-libc-dev
  python-apt-common python3-apt yelp
21 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 4,358 kB of archives.
After this operation, 16.4 kB of additional disk space will be used.
'http://se.archive.ubuntu.com/ubuntu/pool/main/p/python-apt/python-apt-common_2.0.0ubuntu0.20.04.5_all.deb' python-apt-common_2.0.0ubuntu0.20.04.5_all.deb 17052 MD5Sum:a9e11f5f8671c5069f5edaef32e2f620
'http://se.archive.ubuntu.com/ubuntu/pool/main/p/python-apt/python3-apt_2.0.0ubuntu0.20.04.5_amd64.deb' python3-apt_2.0.0ubuntu0.20.04.5_amd64.deb 154164 MD5Sum:8590dd473b444f2756e5c7498e00e7ec
'http://se.archive.ubuntu.com/ubuntu/pool/main/g/gnome-online-accounts/libgoa-1.0-common_3.36.1-0ubuntu1_all.deb' libgoa-1.0-common_3.36.1-0ubuntu1_all.deb 3752 MD5Sum:9252da969452bdf88527829a752ac175

(此輸出被截斷)

假設您想要從上面解析出「乾淨」的 URI,以下sed命令將刪除從第一行到以字串開頭After(含)的所有行。從剩餘的行中,它將刪除空格後的所有內容,然後刪除修改行中的第一個和最後一個字元(這將刪除 URI 周圍的單引號)。

sed '1,/^After/d; s/ .*//; s/.//; s/.$//'

在上面的簡短範例輸出中使用它:

$ sed '1,/^After/d; s/ .*//; s/.//; s/.$//' file
http://se.archive.ubuntu.com/ubuntu/pool/main/p/python-apt/python-apt-common_2.0.0ubuntu0.20.04.5_all.deb
http://se.archive.ubuntu.com/ubuntu/pool/main/p/python-apt/python3-apt_2.0.0ubuntu0.20.04.5_amd64.deb
http://se.archive.ubuntu.com/ubuntu/pool/main/g/gnome-online-accounts/libgoa-1.0-common_3.36.1-0ubuntu1_all.deb

給定相同的輸入數據,命令

sed -n "s,.*\(http://[^']*\).*,\1,p" file

也會起作用。這嘗試匹配以http://單引號開頭和之前結束的任何子字串。然後它用該子字串替換整行並列印修改後的行。不匹配的行將被丟棄。

相關內容