The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

If you can read me, I'm broken!

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:

And it returns this:

grep refresh rate.png


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:

grep all digits.png


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:

grep colored.png

⇧ How to disable line wrap in a terminal? How to delete CPU-stats from cpufreq?  β‡©