27
votes

The following command is working as expected.

# some command | awk '/(\<^create\>|\<^alter\>|\<^drop\>)/,/;/' 
create table todel1 (id int) max_rows=2
/*!*/;
alter table todel1 engine=InnoDB
/*!*/;
create database common
/*!*/;
create database rules
/*!*/;

But it matches only the lower case "create", "alter" etc. I want to use IGNORECASE switch in the awk statement so that it will return all instances of the search term.

4
The example in the accepted answer is mistakenly evaluating IGNORECASE = 1 as a condition (with side effect), rather than as a statement in a block. This condition is truthy, and will result in every line being printed at least once.mwfearnley

4 Answers

22
votes

Add IGNORECASE = 1; to the beginning of your awk command like so:

bash-3.2$ echo "Create" | awk '/^create/;'
bash-3.2$ echo "Create" | awk 'IGNORECASE = 1;/^create/;'
Create
17
votes

The following line executes an OR test instead of an AND :

echo -e "Create\nAny text" | awk 'IGNORECASE = 1;/^create/;'
Create
Create
Any text

The BEGIN special word solved the problem :

echo -e "Create\nAny text" | awk 'BEGIN{IGNORECASE = 1}/^create/;'
Create

Hope this helps.

Sebastien.

9
votes

For those who have an old awk where the IGNORECASE flag is useless:

Option 1

echo "CreAte" | awk '/^[Cc][Rr][Ee][Aa][Tt][Ee]/'

Option 2 (thanks @mwfearnley)

echo "CreAte" | awk 'tolower($0) ~ /^create/'
8
votes

This is a bit late, but two answers to this question (including the accepted answer) mention doing awk 'IGNORECASE=1;...' - i.e. putting IGNORECASE=1 as a condition, instead of a statement in a block.

This should not be done. It does set the variable as intended, but it also (as unintended) evaluates it as a boolean expression, returning true.

A true condition without a block will cause the line to always be printed. If it happens to match the following pattern, it will also be printed a second time.

What the accepted answer probably meant was awk '{IGNORECASE=1} ...', which sets the IGNORECASE variable on each line of text. This can be further improved by using the BEGIN condition to assign it only once. But a cleaner solution is to use the -v flag to set the parameter outside of the script logic:

awk -v IGNORECASE=1 '/(\<^create\>|\<^alter\>|\<^drop\>)/, /;/'

Note that IGNORECASE is specific to gawk. For a non gawk-specific method, the GNU Awk User's Guide suggests using tolower in a pattern match:

awk '(tolower($0) ~ /(\<^create\>|\<^alter\>|\<^drop\>)/), /;/'