Case-insensitive search in awk
Replace your expression to match a pattern (i.e. /&&&key word&&&/
) by another expression explicitly using $0
, the current line:
tolower($0) ~ /&&&key word&&&/
or
toupper($0) ~ /&&&KEY WORD&&&/
so you have
awk 'tolower($0) ~ /&&&key word&&&/ { p = ! p ; next }; p' text.txt
You need single quotes because of the $0
, the BEGIN block can be removed as variables are initialised by default to ""
or 0
on first use, and {print}
is the default action, as mentioned in the comments below.
gawk has an IGNORECASE
builtin variable, which, if set to nonzero, causes all string and regular expression comparisons to be case-insensitive. You could use that:
BEGIN{IGNORECASE=1}
/&&&key word&&&/ { foo bar baz }
etc. This is specific to gawk
, though, but I find it to be more readable than the (more portable) alternative by meuh. Whether that's a problem is, of course, fully up to you.