The following command is working as expected.
# some command | awk \'/(\\<^create\\>|\\<^alter\\>|\\<^drop\\>)/,/;/\'
create table todel1
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.
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
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/'
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\>)/), /;/'