Using Raku (formerly known as Perl_6)
No value-checking:
raku -ne 'if m:g/ ( \d**1..3 )**4 % "." / { $/.join(",").put } else {"n.a.".say};'
OR
raku -ne 'm:g/ ( \d**1..3 )**4 % "." / ?? $/.join(",").put !! "n.a.".say;'
Sample Input:
blabla [11.335.2.33] xyuoeretrete [43.22.11.88] jfdfjkfbs [55.66.77.88]
blabla [66.223.44.33]
foo bar
blabla [1.2.33.3] xyuoeretrete [42] bla[1.32.2.4]
Sample Output (both examples):
11.335.2.33,43.22.11.88,55.66.77.88
66.223.44.33
n.a.
1.2.33.3,1.32.2.4
At least in Perl-family languages, what you're asking for is a match
, not grep. hence use of the m/.../
match operator, made 'global' as in m:g/.../
to return multiple instances of the match [which differs from grep, which returns the full element (such as a line, etc.), containing a match].
Briefly, clusters of 1-to-3 digits ( \d**1..3
) are searched for, with these digits **4
repeated 4 times, and having % "."
a period between each instance, and this regex match is searched for globally (m:global
or m:g
), meaning to get all instances of the match per element (line, etc.), not just the first match. First example: if found (if
) output
the match contained in the $/
match variable, else
say n.a.
. Second example: same match conditional used in Raku's ternary operator, i.e. condition ?? True !! False
. Thus if the conditional is ??
(True), output
the match contained in the $/
match variable, if the conditional is !!
False say n.a.
.
Below WITH value-checking:
raku -ne 'if m:g/ ( \d**1..3 <?{ $/ < 256 }> )**4 % "." / { $/.join(",").put } else {"n.a.".say};'
OR
raku -ne 'm:g/ ( \d**1..3 <?{ $/ < 256 }> )**4 % "." / ?? $/.join(",").put !! "n.a.".say;'
Sample Input: same as above
Sample Output (both examples):
43.22.11.88,55.66.77.88
66.223.44.33
n.a.
1.2.33.3,1.32.2.4
Immediately above, Raku code is shown that checks each 1-to-3 digit cluster to ensure it is an integer less than 256
. The additional regex element, <?{ $/ < 256 }>
, is a positive assertion containing a {...}
code-block that checks if $/
match-variable is less than 256. Reference here.
https://raku.org