简体   繁体   中英

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.

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM