grep: retain colors when piping

By default grep -color is not producing color symbols if it detects that the output is not a terminal.

grep -color=auto is doing the same, that is not producing color symbols if it detects that the output is not a terminal.

The solution is to use “grep -color=always“ in the place that need it.

Do not put it in an alias of grep as it would break some code somewhere else. Color symbols are strings like “ESC[35m” and they will be inserted in the text.

Recursive grep FTW

In case you’re trying to search recursively for a specific file type and data mask, grep -r is not enough to bring the desired result.
You’ll have to use –include ‘type’ and then, even if the directory does not contain any of the given filetype in root, it’s still working.

Example;

grep -r --include '*.extension' 'Text to match' directory
grep -r --include '*.cpp' 'Audio' .

Compare two json in shell

I did it using the JQ command line tool from https://stedolan.github.io/jq/

Exemple: list all json files from current directory and print the difference with updated jsons from updated/ directory

for user in  `ls updated-users`
do
    # print file name
    echo $user
    # simple 
    diff <(jq -S . users/$user) <(jq -S . updated-users/$user)
    # or full on one side and the diff on the other side
    # diff -y --left-column <(jq -S . users/$user) <(jq -S . updated-users/$user)
    # or full on one side and the diff on the other side, colored
    # diff -y --left-column --color  <(jq -S . users/$user) <(jq -S . updated-users/$user)
done

#shell #diff #json #script