7

I find that sometimes, ANSI escaping chars in mvn output prevents me to do this search with grep:

mvn | grep -P "\[INFO\]"

I have to use "\[.*INFO.*\]" to get results.

How to disable processing the ANSI escaping chars? I think there is some config for this?

2 Answers 2

10

There's also --batch-mode (-B) for mvn which makes the output more suitable for e.g. CI or searches likes this as it's more automated.

Or, a generic solution, strip ANSI colors in any command by using ansi2txt tool, which is part of colorized-logs package:

mvn | ansi2txt | grep -P "\[INFO\]"
3
  • Thanks, those are also good alternatives. For local I want to search and check color, for CI maybe -B makes more sense. And ansi2txt is a more general solution, in environments where I can install it.
    – WesternGun
    Commented May 25, 2023 at 11:53
  • @WesternGun: If the output isn't too huge, you could let it output the full text on your terminal in colour, and use your terminal's search within scrollback history. (control-shift-f in KDE's Konsole. Various other terminals have that feature, including screen or tmux which you can run inside any terminal.) The answer you posted runs mvn with uncolorized output when piping to grep, so you're still not getting colourized output through grep :/ Commented May 26, 2023 at 6:27
  • 1
    Yes you are right. After piping to grep the color is not preserved. But I use it in a script so maybe that does not matter. I see surprisingly this answer got more votes than I imagined and you have more upvotes than me so let's accept yours! Good job.
    – WesternGun
    Commented May 26, 2023 at 13:36
6

Seems mvn has a config of -Dstyle.color=auto to ignore the ANSI color escaping chars so that I don't need to alter the pattern in my script to add .*.

# define an alias including the color settings
$ alias mvn='/opt/data/maven/bin/mvn -Dstyle.color=auto'

# use the alias in the script
$ mvn | grep -P "\[INFO\]"
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.132 s
[INFO] Finished at: 2023-05-25T14:05:13+03:00
[INFO] ------------------------------------------------------------------------
2
  • Presumably "auto" means use color only when stdout is a terminal. I'm surprised this isn't the default. And if it has a config file you should be able to enable it there, rather than the alias (which won't be used in scripts).
    – Barmar
    Commented May 26, 2023 at 14:03
  • Yes I think the same, strangely not like grep color settings, which is put into environment vars. We have "always/auto/never" in this settings.
    – WesternGun
    Commented May 29, 2023 at 6:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .