Как рекурсивно извлекать URL-адреса - PullRequest
1 голос
/ 04 августа 2020

Я хочу перечислить все конечные точки в списке URL-адресов, например

https://test123.com/endpoint1/endpoint2/endpoint3
https://test456.com/endpoint1/endpoint2/endpoint3
https://test789.com/endpoint1/endpoint2/endpoint3

, на выходе, например

https://test123.com/
https://test123.com/endpoint1/
https://test123.com/endpoint1/endpoint2/
https://test123.com/endpoint1/endpoint2/endpoint3
https://test456.com/
https://test456.com/endpoint1/
https://test456.com/endpoint1/endpoint2/
https://test456.com/endpoint1/endpoint2/endpoint3

И так далее, рекурсивно перечисляя все конечные точки, чтобы я что-то сделал с каждая конечная точка.

Я пытался использовать это, но он распечатывает его отдельно. awk '$1=$1' FS="/" OFS="\n"

спасибо

Ответы [ 4 ]

3 голосов
/ 04 августа 2020

Не могли бы вы попробовать следующее, написанное и протестированное на показанных примерах.

awk '
match($0,/http[s]?:\/\/[^/]*\//){
  first=substr($0,RSTART,RLENGTH)
  val=substr($0,RSTART+RLENGTH)
  num=split(val,array,"/")
  print first
  for(i=1;i<=num;i++){
    value=(value?value "/":"")array[i]
    print first value
  }
  val=first=value=""
}'  Input_file

Пояснение: Добавление подробного объяснения к вышеизложенному.

awk '                                          ##Starting awk program from here.
match($0,/http[s]?:\/\/[^/]*\//){              ##Using match function which matches http OR https :// then till first occurrence of /
  first=substr($0,RSTART,RLENGTH)              ##Creating first with sub-string which starts from RSTART till RLENGTH value of current line.
  val=substr($0,RSTART+RLENGTH)                ##Creating val which has rest of line out of match function in 3rd line of code.
  num=split(val,array,"/")                     ##Splitting val into array with delimiter / here.
  print first                                  ##Printing first here.
  for(i=1;i<=num;i++){                         ##Starting for loop till value of num from i=1 here.
    value=(value?value "/":"")array[i]         ##Creating value which has array[i] and keep adding in its previous value to it.
    print first value                          ##Printing first and value here.
  }
  val=first=value=""                           ##Nullify variables val, first and value here.
}
'  Input_file                                  ##Mentioning Input_file name here.
2 голосов
/ 04 августа 2020

С двумя петлями:

awk '{
       x=$1 OFS $2 OFS $3            # x contains prefix https://
       for(i=3; i<=NF; i++) {        # NF is number of last element
         printf("%s", x)             # print prefix
         for(j=4; j<=i; j++){
           printf("%s%s", OFS, $j)   # print / and single element
         }
         print ""
       }
     }' FS='/' OFS='/' file

Вывод:

https://test123.com
https://test123.com/endpoint1
https://test123.com/endpoint1/endpoint2
https://test123.com/endpoint1/endpoint2/endpoint3
https://test456.com
https://test456.com/endpoint1
https://test456.com/endpoint1/endpoint2
https://test456.com/endpoint1/endpoint2/endpoint3
https://test789.com
https://test789.com/endpoint1
https://test789.com/endpoint1/endpoint2
https://test789.com/endpoint1/endpoint2/endpoint3

См .: 8 мощных встроенных переменных Awk - FS , OFS , RS, ORS, NR, NF , FILENAME, FNR

1 голос
/ 04 августа 2020
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...