From 4823b8018688092416b3cd4ba9f4f7f86789eedd Mon Sep 17 00:00:00 2001
From: johannst Any valid awk An example is the regex pattern awk provides two special patterns, expr
can be a pattern
./abc/ { print $1 }
which prints the first
+field if the record matches the regex /abc/
. This form is actually a short
+version for $0 ~ /abc/ { print $1 }
, see the regex comparison operator
+below.Special pattern
BEGIN
and END
, which can be used
multiple times. Actions with those patterns are executed exactly once.
%T
alias for %H:%M:%S
S ~ R
, S !~ R
The regex comparison operator, where the former returns true if the string
+S
matches the regex R
, and the latter is the negated form.
+The regex can be either a
+constant
+or dynamic
+regex.
awk '!/^#/ { print $1 }' <file>
Matches records not starting with #
.
echo -e "a\nFOO\nb\nc\nBAR\nd" | \
+ awk '/FOO/,/BAR/ { print }'
+
+/FOO/,/BAR/
define a range pattern of begin_pattern, end_pattern
. When
+begin_pattern
is matched the range is turned on and when the
+end_pattern
is matched the range is turned off. This matches every record
+in the range inclusive.
An exclusive range must be handled explicitly, for example as follows.
+echo -e "a\nFOO\nb\nc\nBAR\nd" | \
+ awk '/FOO/,/BAR/ { if (!($1 ~ "FOO") && !($1 ~ "BAR")) { print } }'
+
echo 'a b c d e f' | awk '{ print $NF $(NF-1) }'
--
cgit v1.2.3