gpt4 book ai didi

awk 打印 $2 符合我的预期,但是当我将 $2 的值与相同的值进行比较时,它返回 false

转载 作者:行者123 更新时间:2023-12-02 18:26:44 25 4
gpt4 key购买 nike

当我发出以下命令时 -

ip -br -c 地址显示 | awk '{print $2}'

它返回类似这样的输出 -

UNKNOWN
UP
DOWN
UP
UP
UP
UP

但是当我只想打印 UP 的内容时,我说 -

ip -br -c 地址显示 | awk '$2 == "UP"'

它不返回任何内容。我已经使用 awk 进行此类比较并且它有效,这里我想知道可能 -br 不返回可比较的字符串。或者我做错了什么。

最佳答案

对于显示的示例,请尝试遵循 GNU grep 代码。

ip -br -c addr show | grep -oP '^\S+[^A-Z]+UP\D+.*?\K(\d+\.){3}\d+'

解释:简单的解释是,运行ip -br -c addr show命令并将其输出作为标准输入发送到grep 命令。在 GNU grep 代码中,使用 oP 选项仅打印匹配的值并启用 PCRE 正则表达式引擎。在主程序中提到正则表达式(其解释在下面添加)并仅打印匹配的部分。

正则表达式说明:

^\S+[^A-Z]+UP\D+.*? ##Matching non-spaces from starting of line followed by non-alphabets followed by UP followed by NON-DIGITS(1 or more occurrences) followed by a lazy match of next pattern.
\K ##\K will forget the previous matched value and will print only further mentioned regex value.
(\d+\.){3}\d+ ##Matching digits(1 or more occurrences) followed by a dot and matching this group 3 times followed by 1 or more occurrences of digits.

关于awk 打印 $2 符合我的预期,但是当我将 $2 的值与相同的值进行比较时,它返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70031769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com