awkipifconfig

awk prints $2 to what I expect, but when I compare $2's value to same it returns false


When I am giving the following command -

ip -br -c addr show | awk '{print $2}'

It returns something like this output -

UNKNOWN
UP
DOWN
UP
UP
UP
UP

but when I want to only print the ones which are UP and I say -

ip -br -c addr show | awk '$2 == "UP"'

it does not return anything. I have used awk with such comparisons and it works, here I am wondering may be -br does not return a comparable string. Or is there something I am doing wrong.


Solution

  • This is because you tell ip command to add color codes to your output by using the -c option:

    -c[color][={always|auto|never}
            Configure color output.

    Remove -c, and ip -br addr show | awk '$2 == "UP"' will work.

    Or, you may match the green color code:

    ip -br -c addr show | awk  '$2 == "\033[32mUP"'
    

    Output: enter image description here