仅在匹配另一个术语的行上进行一个术语
我想在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论