仅在匹配另一个术语的行上进行一个术语

发布于 2025-01-24 10:14:37 字数 1440 浏览 1 评论 0原文

我想在GREP中突出显示我成功完成的术语。我还想突出显示其他术语(up,lower_up),但前提是它在与上学期匹配的行上。但是,我不想突出整个行。

示例输入:

lo: blah blah UP,LOWER_UP
eth0: blah blah UP,LOWER_UP
blah0: blah blah
blah1: blah something something UP

所需的输出(**表示颜色突出显示)

lo: blah blah UP,LOWER_UP
**eth0**: blah blah **UP,LOWER_UP**
blah0: blah blah
blah1: blah something something UP

我当前的进度(**表示颜色突出显示): UP,Lower_up在行以lo:开始,突出显示了我不想要的。

lo: blah blah **UP,LOWER_UP**
**eth0**: blah blah **UP,LOWER_UP**
blah0: blah blah
blah1: blah something something UP

我当前的代码:

connected_interfaces="" # we will add terms here to grep them

cd /sys/class/net || exit 1
for i in *;do
    if cat "/sys/class/net/$i/carrier" &>/dev/null; then # don't try to process invalid files
        if grep -q 1 "/sys/class/net/$i/carrier"; then
            if [[ $i != "lo" ]]; then
                if [[ $connected_interfaces == "" ]]; then
                    connected_interfaces="$i:"
                else
                    connected_interfaces="$connected_interfaces|$i:"
                fi
            fi
        fi
    fi
done
GREP_COLOR='1;32;33' ip link show | grep --color=always -E "$connected_interfaces|$" | GREP_COLOR='1;32;32' grep --color -E "UP,LOWER_UP|$"

我不想简单剪切非匹配行,因为我喜欢看到系统上的所有接口。

I want to highlight terms in grep, which I've successfully done. I also want to highlight other terms (UP,LOWER_UP) but only if it is on a line matching the previous term. However, I don't want to highlight the entire line.

Example input:

lo: blah blah UP,LOWER_UP
eth0: blah blah UP,LOWER_UP
blah0: blah blah
blah1: blah something something UP

Desired output (** means colour highlighted)

lo: blah blah UP,LOWER_UP
**eth0**: blah blah **UP,LOWER_UP**
blah0: blah blah
blah1: blah something something UP

My current progress (** means colour highlighted):
UP,LOWER_UP is highlighted on the line starting with lo: which I don't want.

lo: blah blah **UP,LOWER_UP**
**eth0**: blah blah **UP,LOWER_UP**
blah0: blah blah
blah1: blah something something UP

My current code:

connected_interfaces="" # we will add terms here to grep them

cd /sys/class/net || exit 1
for i in *;do
    if cat "/sys/class/net/$i/carrier" &>/dev/null; then # don't try to process invalid files
        if grep -q 1 "/sys/class/net/$i/carrier"; then
            if [[ $i != "lo" ]]; then
                if [[ $connected_interfaces == "" ]]; then
                    connected_interfaces="$i:"
                else
                    connected_interfaces="$connected_interfaces|$i:"
                fi
            fi
        fi
    fi
done
GREP_COLOR='1;32;33' ip link show | grep --color=always -E "$connected_interfaces|
quot; | GREP_COLOR='1;32;32' grep --color -E "UP,LOWER_UP|
quot;

I don't want to simple cut out non matching lines because I like seeing all the interfaces on a system.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文