extract a string after a pattern
This may work for you:
awk -F "[):,]" '{ for (i=2; i<=NF; i++) if ($i ~ /id/) print $2, $(i+1) }' file
Results:
03 20403 49103 2904 20904 30105 20
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/bashawk 'BEGIN{FS="[\(\):,]"}/client_id/ {cid="no_client_id"for (i=1; i<NF; i++) { if ($i == "client_id") { cid = $(i+1) } else if ($i == "id") { id = $(i+1); print cid OFS id; } }}' input_file_name
Output:
03 20403 49103 2904 20904 30105 20
Explanation:
awk 'BEGIN{FS="[\(\):,]"}
: invokeawk
, use(
)
:
and,
as delimiters to separate your fields/client_id/ {
: Only do the following for the lines that containclient_id
:for (i=1; i<NF; i++) {
: iterate through the fields on each line one field at a timeif ($i == "client_id") { cid = $(i+1) }
: if the field we are currently on isclient_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 isid
, then print theclient_id : id
pair ontostdout
input_file_name
: supply the name of your input file as first argument to theawk
script.
This might work for you (GNU sed):
sed -r '/.*(\(client_id:([0-9]+))[^(]*\(id:([0-9]+)/!d;s//\2 \3\n\1/;P;D' file
/.*(\(client_id:([0-9]+))[^(]*\(id:([0-9]+)/!d
if the line doesn't have the intended strings delete it.s//\2 \3\n\1/
re-arrange the line by copying theclient_id
and moving the firstid
ahead thus reducing the line for successive iterations.P
print upto the introduced newline.D
delete upto the introduced newline.