extract a string after a pattern

后端 未结 4 984
失恋的感觉
失恋的感觉 2021-02-09 11:33

I want to extract the numbers following client_id and id and pair up client_id and id in each line.

For example, for the following lines of log,

User(cli         


        
4条回答
  •  情话喂你
    2021-02-09 12:12

    Here's a awk script that works (I put it on multiple lines and made it a bit more verbose so you can see what's going on):

    #!/bin/bash
    
    awk 'BEGIN{FS="[\(\):,]"}
    /client_id/ {
    cid="no_client_id"
    for (i=1; i

    Output:

    03 204
    03 491
    03 29
    04 209
    04 301
    05 20
    

    Explanation:

    • awk 'BEGIN{FS="[\(\):,]"}: invoke awk, use ( ) : and , as delimiters to separate your fields
    • /client_id/ {: Only do the following for the lines that contain client_id:
    • for (i=1; i: iterate through the fields on each line one field at a time
    • if ($i == "client_id") { cid = $(i+1) }: if the field we are currently on is client_id, then its value is the next field in order.
    • else if ($i == "id") { id = $(i+1); print cid OFS id;}: otherwise if the field we are currently on is id, then print the client_id : id pair onto stdout
    • input_file_name: supply the name of your input file as first argument to the awk script.

提交回复
热议问题