I need to search for a keyword using awk, but I want to perform a case-insensitive (non case sensitive) search.
I think the best approach is to capitalize both the search term ("key word") and the target line that awk is reading at the same time. From this question I how to use toupper
to print in all uppercase, but I don't know how to use it in a match because that answer just shows printing and doesn't leave the uppercase text in a variable.
Here is an example, given this input:
blablabla
&&&Key Word&&&
I want all
these text and numbers 123
and chars !"£$%&
as output
&&&KEY WORD&&&
blablabla
I'd like this output:
I want all
these text and numbers 123
and chars !"£$%&
as output
This is what I have, but I don't know how to add in toupper
:
awk "BEGIN {p=0}; /&&&key word&&&/ { p = ! p ; next } ; p { print }" text.txt
awk 'toupper($0)~/&&&KEY WORD&&&/ { p = ! p ; next } ; p;' text.txt
. There's no need for theBEGIN
block and since the default action is to print,p;
is enough. – terdon Apr 01 '16 at 12:35BEGIN
block" since an uninitialized variable evaluates as false. – glenn jackman Apr 01 '16 at 13:44tolower
is present on ancient (or not so ancient) awk versions (ex: AIX) systems, buttoupper
is not always available ^^. – Olivier Dulac Apr 01 '16 at 20:20