Views:
1,088β
Votes: 2β
Tag :
grep
Link:
π See Original Answer on Ask Ubuntu β§ π
URL:
https://askubuntu.com/q/1138690
Title:
grep change color highlight from first line to second line
ID:
/2019/04/27/grep-change-color-highlight-from-first-line-to-second-line
Created:
April 27, 2019
Edited: June 12, 2020
Upload:
September 15, 2024
Layout: post
TOC:
false
Navigation: false
Copy to clipboard: false
grep
on EOL (End of Line character)
Running grep
a second time to highlight a different word causes all other lines without the word to disappear. The secret is to grep
on different word OR the EOL character (\$
or simply $
). The full explanation is here:
The new command becomes:
xrandr --current | grep -e " connected" -A1 | grep -E '\*|$'
:
And it returns this:
$
represents searching for EOL (End of Line) character which is on every line. This means every line from the firstgrep
reappears on the secondgrep
. The EOL character is not visible so you donβt seeβ
highlighted at the end of every line.- Because the second
grep
is searching for*
(which is a control character), it needs to be prepended with\
. Normally you would simply useword|$
to search and not\word|$
. The asterisk (or splat)*
character is an exception. See Escaping Meta-Characters.
Bonus Answer 1
Enhance output by highlighting Hz values
Using the answer from: Matching decimal number in grep. You can highlight the actual frequency rate in addition to the *
.
This command:
xrandr --current | grep -e " connected" -A1 | grep -E '[0-9]+\.[0-9]+\*|$'
Gives you this:
Bonus Answer 2
Enhance output with different highlight colors
The default red highlight color may not stand out well on your monitor. I know it is kind of washed out in my gnome-terminal
. From this colored grep blog you can create these aliases:
alias grey-grep="GREP_COLOR='1;30' grep --color=always"
alias red-grep="GREP_COLOR='1;31' grep --color=always"
alias green-grep="GREP_COLOR='1;32' grep --color=always"
alias yellow-grep="GREP_COLOR='1;33' grep --color=always"
alias blue-grep="GREP_COLOR='1;34' grep --color=always"
alias magenta-grep="GREP_COLOR='1;35' grep --color=always"
alias cyan-grep="GREP_COLOR='1;36' grep --color=always"
alias white-grep="GREP_COLOR='1;37' grep --color=always"
For permanent availability (persistent across reboots) add them to your ~/.bashrc
file.
These commands:
xrandr --current | grep -e " connected" -A1 | green-grep -E '[0-9]+\.[0-9]+\*|$'
xrandr --current | grep -e " connected" -A1 | yellow-grep -E '[0-9]+\.[0-9]+\*|$'
xrandr --current | grep -e " connected" -A1 | cyan-grep -E '[0-9]+\.[0-9]+\*|$'
Gives you this:
-
After trying all the colors Iβm leading towards yellow as my favorite.