need to search the pattern and print its next value

Viewed 90

Query: I have file with multiple line with below text, need to fetch 2 entry from each line, APP_ID:686 & APP_SUBSC_ID:68601 fields are not constant its random. Need to search the pattern and print next value.

Question: How can it be done ?

Input file

1.**8340411087390720000**     68601 CSS:css|unidades_promotion:NA|stopc:False|USER-AGENT:Mozilla/5.0 (Linux; Android 6.0.1;Safari/537.36|CONTENT_ID:|cmpSrc:TrafficCompany_MK|omrevposition:|unidades:999999|randomAmtFlag:false|**APP_ID:686**|CIRCLE_ID:1|PACK_NAME:CVAS_44869_SUB|network-type:WIFI|cmpId:Spain_Movistar_Tinder_TrafficCompany|opr:Telefonica-CVAS|**APP_SUBSC_ID:68601**|ctry:ES|ctid:m7RZCi6ANPG2rIMFIIhOCJR5qjLad6o95JVBnr2V8Sc|bypasslimit:|svc:19|APP_NAME:TINDER|sid:m7RZCi6ANPG2rIMFIIhOCJR5qjLad6o95JVBnr2V8Sc|

Code

awk -f "|" '{ for (x=1; x<NF; x++ ) { if ($x == "APP_SUBSC_ID:") {x=x+1; printf("%s\n", $x); break}}}'

Output required:

APP_ID:686 APP_SUBSC_ID:68601
2 Answers

Could you please try following. Written and tested with shown samples in GNU awk.

awk '
match($0,/APP_ID:[0-9]+/){
  id=substr($0,RSTART,RLENGTH)
}
match($0,/APP_SUBSC_ID:[0-9]+/){
  print (id?id:"No id"),substr($0,RSTART,RLENGTH)
  id=""
}' Input_file

Explanation: Adding detailed explanation for above.

awk '                                ##Starting awk program from here.
match($0,/APP_ID:[0-9]+/){           ##using match function to match APP_ID: digits here.
  id=substr($0,RSTART,RLENGTH)       ##Creating id variable which has matched regex value as shown above.
}
match($0,/APP_SUBSC_ID:[0-9]+/){     ##Using match function to match APP_SUBSC_ID: digits here.
  print (id?id:"No id"),substr($0,RSTART,RLENGTH) ##Printing id or no_id(in case its not found) and value of matched sub string.
}
' Input_file                         ##Mentioning Input_file name here.

As per OP's comment for using one liner use:

awk 'match($0,/APP_ID:[0-9]+/){id=substr($0,RSTART,RLENGTH)} match($0,/APP_SUBSC_ID:[0-9]+/){print (id?id:"No id"),substr($0,RSTART,RLENGTH)}' Input_file

I would do it following way using GNU AWK. Let file.txt content be:

1.**8340411087390720000**     68601 CSS:css|unidades_promotion:NA|stopc:False|USER-AGENT:Mozilla/5.0 (Linux; Android 6.0.1;Safari/537.36|CONTENT_ID:|cmpSrc:TrafficCompany_MK|omrevposition:|unidades:999999|randomAmtFlag:false|**APP_ID:686**|CIRCLE_ID:1|PACK_NAME:CVAS_44869_SUB|network-type:WIFI|cmpId:Spain_Movistar_Tinder_TrafficCompany|opr:Telefonica-CVAS|**APP_SUBSC_ID:68601**|ctry:ES|ctid:m7RZCi6ANPG2rIMFIIhOCJR5qjLad6o95JVBnr2V8Sc|bypasslimit:|svc:19|APP_NAME:TINDER|sid:m7RZCi6ANPG2rIMFIIhOCJR5qjLad6o95JVBnr2V8Sc|

then

awk 'BEGIN{FPAT="(APP_ID:[0-9]+)|(APP_SUBSC_ID:[0-9]+)"}{$1=$1;print}' file.txt

output

APP_ID:686 APP_SUBSC_ID:68601

Explanation: I specified FPAT i.e. field pattern, therefore instructing to treat matching elements of line as fields: (APP_ID: followed by one or more digits) or (APP_SUBSC_ID: followed by one or more digits). Then I need to do something so AWK would rebuilt line therefore I do $1=$1 (without it print would print whole unaltered line)

(tested in gawk 4.2.1)

Related