Filter column with awk and regexp

前端 未结 6 964
谎友^
谎友^ 2021-02-01 19:08

I\'ve a pretty simple question. I\'ve a file containing several columns and I want to filter them using awk.

So the column of interest is the 6th column and I want to fi

6条回答
  •  孤独总比滥情好
    2021-02-01 19:49

    I would do the regex check and the numeric validation as different steps. This code works with GNU awk:

    $ cat data
    a b c d e 132x123y
    a b c d e 123S12M
    a b c d e 12S23M
    a b c d e 12S23Mx
    

    We'd expect only the 3rd line to pass validation

    $ gawk '
        match($6, /^([[:digit:]]{1,3})[SM]([[:digit:]]{1,3})[SM]$/, m) && 
        1 <= m[1] && m[1] <= 100 && 
        1 <= m[2] && m[2] <= 100 {
            print
        }
    ' data
    a b c d e 12S23M
    

    For maintainability, you could encapsulate that into a function:

    gawk '
        function validate6() {
            return( match($6, /^([[:digit:]]{1,3})[SM]([[:digit:]]{1,3})[SM]$/, m) && 
                    1<=m[1] && m[1]<=100 && 
                    1<=m[2] && m[2]<=100 );
        }
        validate6() {print}
    ' data
    

提交回复
热议问题