read parameter from a url

Viewed 38

I am trying to read a parameter from the URL, i am able to read for single line but i don't know how to loop in awk, can someone help?

i have file with 1000+ entries like

http://projectreporter.nih.gov/project_info_details.cfm?aid=7714687&icde=0
http://projectreporter.nih.gov/project_info_description.cfm?aid=7896503&icde=0
http://projectreporter.nih.gov/project_info_details.cfm?aid=7895320&icde=0
http://projectreporter.nih.gov/project_info_details.cfm?aid=2675186&icde=9195637

i am trying to only retrive "aid=xxxxxxx", i used the following command to do it and i get the "aid" for the last line

awk '{match($0,"aid=([^ &]+)",a)}END{print a[1]}' file1.txt > outputFile.txt

how to do the same in a loop so i can get all the occurrence? any help would be appreciated

1 Answers

This should work a little fine tuning for your attempted code.

awk 'match($0,/aid[^&]*/){print substr($0,RSTART,RLENGTH)}' Input_file


In case your single line can have multiple occurrences of aid and you want to print all then try following.

awk '
{
  while(match($0,/aid[^&]*/)){
    print substr($0,RSTART,RLENGTH)
    $0=substr($0,RSTART+RLENGTH)
  }
}
' Input_file
Related