regex - How to find the multiline pattern match (they must be first time match)? -
i know question how find patterns across multiple lines using grep? think problem more complicated. need help.
i have dictionary file bcfile
as
boundary { inlet { type fixedvalue; value uniform (5 0 0); } outlet { type inletoutlet; inletvalue $internalfield; value $internalfield; } .... }
i writing script print out inlet
boundary condition fixedvalue
, , outlet
boundary condition inletoutlet
.
if use cat bcfile | grep "type" | awk '{printf $2}' | tr -d ";"
, won't work keyword type
occurs many times.
if use awk -v rs='}' '/inlet/ { print $4 }' bcfile
, won't work either, because keyword inlet
occurs many times.
i need way find pattern first search key word inlet
, search closest {
, }
.
anyone knows how smartly?
since didn't provide expected output input posted we're guessing @ want output how in gnu awk:
$ cat tst.awk begin{ rs="\0" } { print "inlet:", gensub(/.*\yinlet\y[^}]*type\s+(\w+).*/,"\\1","") print "outlet:", gensub(/.*\youtlet\y[^}]*type\s+(\w+).*/,"\\1","") } $ gawk -f tst.awk file inlet: fixedvalue outlet: inletoutlet
explanation:
rs="\0"
= set record separator null string awk reads whole file single record.
gensub(/.*\yinlet\y[^}]*type\s+(\w+).*/,"\\1","")
= word inlet
followed characters except }
(so stop before first }
after inlet
instead of last }
in file) , word type
followed white space. alpha-numeric string after (\w+
) word want printed remember , replace whole record string saved in \\1
.
setting rs="\0"
, gensub()
both gawk-specific.
Comments
Post a Comment