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
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
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.