텍스트에서 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://작은따옴표 로 시작하고 그 앞에서 끝나는 모든 하위 문자열을 일치시키려고 시도합니다 . 그런 다음 전체 줄을 해당 하위 문자열로 바꾸고 수정된 줄을 인쇄합니다. 일치하지 않는 줄은 삭제됩니다.

관련 정보