Example of how to use AWK in order to get/extract/return parts of lines/strings, matching a "dynamic" regular expression (specified through a shell environment variable).
Source file used in this example
$ cat example.txt
Attr[27716, Field[4937190,usage=51]]
Attr[27716, Field[4937191,usage=10]]
Attr[27716, Field[4937192,usage=321]]
Two usages
# Defining the regular expression pattern as environment variable
$ MY_PATTERN="([0-9]+),used"
# Using the predefined awk variables RSTART and RLENGTH
# Returns the whole string matching the regexp
$ awk -v PATTERN="${MY_PATTERN}" 'match($0, PATTERN) { print substr( $0, RSTART, RLENGTH) }' example.txt
4937190,usage
4937191,usage
4937192,usage
# Using the capture group & array functionality in order to get only the regexp part in parenthesis
$ awk -v PATTERN="${MY_PATTERN}" 'match($0, PATTERN, array) { print array[1] }' example.txt
4937190
4937191
4937192
No comments :
Post a Comment