extract a string after a pattern

后端 未结 4 979
失恋的感觉
失恋的感觉 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:27

    I would prefer awk for this, but if you were wondering how to do this with sed, here's one way that works with GNU sed.

    parse.sed

    /client_id/ {
      :a
      s/(client_id:([0-9]+))[^(]+\(id:([0-9]+)([^\n]+)(.*)/\1 \4\5\n\2 \3/
      ta
      s/^[^\n]+\n//
    }
    

    Run it like this:

    sed -rf parse.sed infile
    

    Or as a one-liner:

    Output:

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

    Explanation:

    The idea is to repeatedly match client_id:([0-9]+) and id:([0-9]+) pairs and put them at the end of pattern space. On each pass the id:([0-9]+) is removed.

    The final replace removes left-overs from the loop.

提交回复
热议问题