Issue
Im trying to capture numbers inside a file using AWK, I could capture all, but im not being able to capture those in a certain amount of digits. What im doing wrong?
echo -e "$teste" | awk '/_OA/ { match($0,/\[\([:digit:]{4,13}\]/);oa = substr($0,RSTART,RLENGTH);print oa}'
File sample:
_OA ............. [6712227000168]
_OA Tasdsd, OA .. [91][355016]
_OA Tasdsd, DA .. [91][5512987000]
Expected:
6712227000168
355016
5512987000
Hint for the regex match answers: Thanks so much for all the answers, i found link that I need to use a --posix option because of my awk version.
Solution
Your regexp \[\([:digit:]{4,13}\]
says:
\[
= the literal character[
\(
= the literal character(
[:digit:]
= a bracket expression containing a character set of the characters:
,d
,i
,g
,t
{4,13}
= a regexp interval that's 4 to 13 repetitions of the preceding bracket expression\]
= the literal character]
The 2 main issues with that which are causing your regexp to be unable to match any of your input are:
- You don't have any
(
s in your input (from #2 above), and - To match digits you need a character class
[:digit:]
inside a bracket expression[[:digit:]]
, not a character set:digit:
inside a bracket expression[:digit:]
(from #3 above)
You also don't actually need to escape the ]
at the end of the regexp as it's only a regexp metachar (end of bracket expression) if preceded by a matching unescaped [
(start of bracket expression).
So the regexp I think you wanted to write instead would have been:
\[[[:digit:]]{4,13}]
e.g.:
$ awk '/_OA/ { match($0,/\[[[:digit:]]{4,13}]/);oa = substr($0,RSTART,RLENGTH);print oa}' file
[6712227000168]
[355016]
[5512987000]
or to only print the numbers:
$ awk '/_OA/ { match($0,/\[[[:digit:]]{4,13}]/);oa = substr($0,RSTART+1,RLENGTH-2);print oa}' file
6712227000168
355016
5512987000
Answered By - Ed Morton Answer Checked By - Marilyn (WPSolving Volunteer)