Gawk matching: Difference between revisions
From wikinotes
(Created page with "= match = <blockquote> match checks for a matching string, returns char number if found, otherwise returns a 0 <source lang="awk"> match($0, "searchterm") </source> <source...") |
(→match) |
||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= regex = | |||
<blockquote> | |||
Used to filter lines, like grep. | |||
<syntaxhighlight lang="bash"> | |||
echo ' | |||
1920x1080 | |||
foo | |||
5760x1080 | |||
' | awk '$0 ~ /[0-9]+x[0-9]+/ { print $0 }' | |||
</syntaxhighlight> | |||
</blockquote><!-- regex --> | |||
= match = | = match = | ||
<blockquote> | <blockquote> | ||
Line 5: | Line 17: | ||
<source lang="awk"> | <source lang="awk"> | ||
match($0, "searchterm") | match($0, "searchterm") | ||
if (match($0, "searchterm")) { print "hi" } | |||
</source> | </source> | ||
Line 14: | Line 27: | ||
#> 0 | #> 0 | ||
echo "abcdefg" | awk '{ | echo "abcdefg" | awk '{ | ||
if(match($0, "cd")) { | if(match($0, "cd")) { | ||
print "match found"; | print "match found"; | ||
} | } | ||
}' | }' | ||
#> match found | #> match found | ||
</source> | </source> | ||
The matched text is identified using global variables. | |||
<syntaxhighlight lang="awk"> | |||
RSTART // the first matching character | |||
RLENGTH // the number of matching characters total | |||
</syntaxhighlight> | |||
You can extract the value using <code>substr()</code>. | |||
<syntaxhighlight lang="awk"> | |||
echo "abc_def_hij" | awk '{ | |||
success = match($0, "_[a-z]+_"); | |||
if (success) { | |||
print substr($0, RSTART, RLENGTH); | |||
} | |||
}' | |||
</syntaxhighlight> | |||
</blockquote><!-- awk match --> | </blockquote><!-- awk match --> | ||
= match-ranges = | |||
<blockquote> | |||
Similar to [[sed]], awk can operate on ranges between two matches. | |||
<syntaxhighlight lang="bash"> | |||
# print lines between 'config:' and the end of input | |||
zpool status zroot | awk '/^config:/,/end/ { print $0 }' | |||
</syntaxhighlight> | |||
</blockquote><!-- match-ranges --> | |||
= Substitution = | |||
<blockquote> | |||
<syntaxhighlight lang="awk"> | |||
foo = "/dev/ada0p1" | |||
sub(/p[0-9]+$/, "", $foo) // search/replace first | |||
gsub(/a/, "b", $foo) // search/replace all | |||
</syntaxhighlight> | |||
</blockquote><!-- Substitution --> |
Latest revision as of 00:22, 17 July 2022
regex
Used to filter lines, like grep.
echo ' 1920x1080 foo 5760x1080 ' | awk '$0 ~ /[0-9]+x[0-9]+/ { print $0 }'
match
match checks for a matching string, returns char number if found, otherwise returns a 0
match($0, "searchterm") if (match($0, "searchterm")) { print "hi" }echo "abcdefg" | awk '{var=match($0, "cd"); print var}' #> 3 echo "abcdefg" | awk '{var=match($0, "zef"); print var}' #> 0 echo "abcdefg" | awk '{ if(match($0, "cd")) { print "match found"; } }' #> match foundThe matched text is identified using global variables.
RSTART // the first matching character RLENGTH // the number of matching characters totalYou can extract the value using
substr()
.echo "abc_def_hij" | awk '{ success = match($0, "_[a-z]+_"); if (success) { print substr($0, RSTART, RLENGTH); } }'
match-ranges
Similar to sed, awk can operate on ranges between two matches.
# print lines between 'config:' and the end of input zpool status zroot | awk '/^config:/,/end/ { print $0 }'
Substitution
foo = "/dev/ada0p1" sub(/p[0-9]+$/, "", $foo) // search/replace first gsub(/a/, "b", $foo) // search/replace all