ignorecase in AWK

后端 未结 4 796
轮回少年
轮回少年 2020-12-03 07:32

The following command is working as expected.

# some command | awk \'/(\\<^create\\>|\\<^alter\\>|\\<^drop\\>)/,/;/\' 
create table todel1         


        
相关标签:
4条回答
  • 2020-12-03 07:35

    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.

    0 讨论(0)
  • 2020-12-03 07:52

    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
    
    0 讨论(0)
  • 2020-12-03 07:56

    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/'
    
    0 讨论(0)
  • 2020-12-03 08:01

    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\>)/), /;/'
    
    0 讨论(0)
提交回复
热议问题