Is it possible to colorize the output of gnu in parallel with grep?

I have a script that runs git commands on several repositories in parallel, which gnu are parallel. I would like to pass the output of the git command through grep and the color of certain parts, for example, in git status. I want the word "clean" to appear in green. Is there a way to do this with gnu parallel and grep?

This is my script:

#!/bin/bash START_DIR=`pwd` export GIT_ARGS=$* function do_git() { PROJECT_DIR=`dirname $1` cd $PROJECT_DIR echo "" pwd git $GIT_ARGS echo "" cd $START_DIR } export -f do_git find . -maxdepth 2 -type d -name ".git" | sort | parallel --max-procs 4 "do_git {}" 
+4
source share
3 answers

Try adding this to the end of your pipeline:

 | grep -E --color 'clean|word1|word2|$' 

Replace and add or remove words as needed. $ causes all lines to match and pass. The --color option is for GNU grep . Other versions of grep may use a different option.

In addition, there are several utilities that can perform coloring.

General tips:

  • Avoid using all-caps variable names to prevent collision of names with shell variables
  • Use $() instead of backticks - they are more readable and more universal (for example, nesting)
  • The use of the function keyword is not required.
  • See BashFAQ / 028 regarding the use of the location of your script
  • I don't think GIT_ARGS needs to be exported
+4
source

To make grep display colors when used in parallel try grep --color=always

+1
source

I will see if I can give a good suggestion on how to show the color.

Meanwhile, I think you could improve your script as follows:

 #!/bin/bash function do_git { PROJECT_DIR=${1%.git} cd "$PROJECT_DIR" echo pwd git "${@:2}" echo } export -f do_git find . -maxdepth 2 -type d -name '.git' | sort | parallel --max-procs 4 do_git '{}' " $@ " 

You do not need to change back with cd "$ START_DIR", since it starts in a subshell (possibly in parallel) and will not affect the calling shell.

0
source

Source: https://habr.com/ru/post/1499639/


All Articles