Noor
Noor

Reputation: 49

how to color the output for two different strings in two different files in Bash

I have two files that will always hold different strings, I want to know a way to print out everything in those two files and highlight only the differences between them.

$cat file1.txt
fox:1
bike:3
beer:21

$cat file2.txt
fox:1
beer:22
bike:3

$pr -m -t file1.txt file2.txt
fox:1                         fox:1
beer:21                       bike:3
bike:3                        beer:22

I want beer:21 beer:22 to be highlighted in red. keep in mind that beerstring is not always there in one of the files or existed but in a different name like apple:0

Upvotes: 2

Views: 291

Answers (1)

Farvardin
Farvardin

Reputation: 5424

You can use diff and set color for changed lines:

diff --old-group-format=$'\e[0;31m%<\e[0m' 
     --new-group-format=$'\e[0;31m%>\e[0m'      
     --unchanged-group-format=$'\e[0;32m%=\e[0m'    file1 file2

Upvotes: 4

Related Questions