Yogesh
Yogesh

Reputation: 673

Bash: Selected Part in BOLD font in bash shell

Is there any way to grep some text from large file and mark that in BOLD letters in Linux BASH shell ? Like

Filesystem Size Used Avail Use% Mounted on
/dev/sda1 15G 11G 3.3G 76% /
tmpfs 7.9G 0 7.9G 0% /dev/shm
/dev/sda3 51G 45G 3.8G 93% /home
/dev/sdc1 917G 359G 512G 42% /data

I have above output and I want whenever system mails me about this df output the /data line should be in bold letters.

Upvotes: 1

Views: 2723

Answers (2)

anubhava
anubhava

Reputation: 786349

Using ANSI escape sequence you can do this (though this is terminal dependent):

echo -e "\033[1m$(grep '/data' file)\033[0m"

Will produce:

/dev/sdc1 917G 359G 512G 42% /data

Upvotes: 3

Oliver Matthews
Oliver Matthews

Reputation: 7863

Bash doesn't really have bold (it does have bright which is effectively equivilent). You could use sed to insert the bash control codes, but if you are less concerned with the exact colour choice, you can use

grep --color -E "text match pattern|" mylargefile

To get grep to do the highlighting. (see Colorized grep -- viewing the entire file with highlighted matches for alternatives and discussion)

Upvotes: 1

Related Questions