Im able to capture all values between parentheses with awk expression
'NR>1{print $1}' RS='(' FS=')'
But im struggling to match one in specific, where i do not want to match by line number but by a string. Not sure if that is possible with awk.
Original file content is
if ($remote_addr ~ ^(1.2.3.4|5.6.7.8)$) {
set $maintenance off;
}
if ($maintenance = on) {
return 503;
}
where there are multiple parentheses in different orders.
I need to extract content 1.2.3.4|5.6.7.8 from the line
if ($remote_addr ~ ^
(or simply in the line that contains $remote_addr).
ifstatement? Also, do ou want to extract everything in theif ( ... )parentheses, or again a substring of that? You stated you want the1.2.3.4|5.6.7.8in case of theif ($remove_addr ~ ... )line, but what about theif ($maintenance = ...)line?$remote_addrin other contexts in your sample input/output as it's always trivial to match the lines you want but much harder to not match similar lines you don't want (e.g..*will match any line but is rarely the right answer). For example, add lines like$remote_addr = 1.2.3.4andif ( ($remote_addr ~ ^(1.2.3.4|5.6.7.8)$) && (whatever) ) {...}andif ( (whatever) && ($remote_addr ~ ^(1.2.3.4|5.6.7.8)$) ) {...}to provide more useful test cases.