When it comes to command line text processing, from an abstract point of view, there are three major pillars



Download 125.91 Kb.
Page12/60
Date09.03.2023
Size125.91 Kb.
#60849
1   ...   8   9   10   11   12   13   14   15   ...   60
Learn GNU AWK

Grouping


Often, there are some common things among the regular expression alternatives. It could be common characters or qualifiers like the anchors. In such cases, you can group them using a pair of parentheses metacharacters. Similar to a(b+c)d = abd+acd in maths, you get a(b|c)d = abd|acd in regular expressions.
$ # without grouping $ printf 'red\nreform\nread\narrest\n' | awk '/reform|rest/' reform arrest $ # with grouping $ printf 'red\nreform\nread\narrest\n' | awk '/re(form|st)/' reform arrest $ # without grouping $ printf 'sub par\nspare\npart time\n' | awk '/\
|\
/' sub par part time $ # taking out common anchors $ printf 'sub par\nspare\npart time\n' | awk '/\<(par|part)\>/' sub par part time $ # taking out common characters as well $ # you'll later learn a better technique instead of using empty alternate $ printf 'sub par\nspare\npart time\n' | awk '/\
/' sub par part time

Matching the metacharacters


You have seen a few metacharacters and escape sequences that help to compose a regular expression. To match the metacharacters literally, i.e. to remove their special meaning, prefix those characters with a \ character. To indicate a literal \ character, use \\.
Unlike grep and sed, the string anchors have to be always escaped to match them literally as there is no BRE mode in awk. They do not lose their special meaning when not used in their customary positions.
$ # awk '/b^2/' will not work even though ^ isn't being used as anchor $ # b^2 will work for both grep and sed if you use BRE syntax $ echo 'a^2 + b^2 - C*3' | awk '/b\^2/' a^2 + b^2 - C*3 $ # note that ')' doesn't need to be escaped $ echo '(a*b) + c' | awk '{gsub(/\(|)/, "")} 1' a*b + c $ echo '\learn\by\example' | awk '{gsub(/\\/, "/")} 1' /learn/by/example
Backreferences section will discuss how to handle the metacharacters in replacement section.

Download 125.91 Kb.

Share with your friends:
1   ...   8   9   10   11   12   13   14   15   ...   60




The database is protected by copyright ©ininet.org 2024
send message

    Main page