hedleyyan hedleyyan - 26 days ago 7
Bash Question

How to use backreference multiple times?

dump.data as below:

x"xxx":111,"xxxx":2222,xxxx"id":123,xxxxxx"value":321,xxxxxx"id":234,xxxxxx"value":432,xx
x"xxx":yyy,"xxxx":zzz,xxxx"id":223,xxxxxx"value":221,xxxxxx"id":224,xxxxxx"value":232,xxxx....


I want to filer only the
id
and
value
info. e.g. After whatever process gives me this:

"id":123,"value":321,"id":234,"value":432
"id":223,"value":221,"id":224,"value":232





What comes to my mind is using backreference, i.e. sed:

sed 's|.*\("id":[0-9]*\).*\("value":[0-9]*\).*|\1,\2|' dump.data


which gives me this:

"id":234,"value":432
"id":224,"value":232


But how can I get all the id and value info?

Answer

Simplified version from other solutions already presented

$ perl -lne 'print join ",", /"[^"]+":\d+/g' dump.data 
"id":123,"value":321,"id":234,"value":432
"id":223,"value":221,"id":224,"value":232
  • "[^"]+":\d+ pattern to extract
  • join "," use , as separator to combine the extracted patterns


To capture only id and value

$ perl -lne 'print join ",", /"(?:id|value)":\d+/g' dump.data
"id":123,"value":321,"id":234,"value":432
"id":223,"value":221,"id":224,"value":232
  • (?:id|value) to restrict to only id or value, but in non-capturing group so that only the entire extracted text is passed onto join