I want to filter standard error for a command that prints status information to standard error. In this case, Borg Backup. Specifically, the default output of borg check includes lines like:

Checking segments ...

sent to standard error. I want to filter those lines out, because they are a nuisance for a cron job. Currently (as of 1.0.2) it is not possible to disable this output via borg configuration.

I used the approach described by Gilles. This works fine for a local Borg repository, for example:

{ borg check '/media/faheem/My Passport/backup-Mail' 2>&1 1>&3 | tr '\r' '\n' | grep -v "^Checking segments" 1>&2; } 3>&1

However, the situation is a little different for a remote repository. The output looks like

Remote: Checking segments 99.3% 

How would one modify the reverse grep to get rid of both of these cases?

{ borg check faheem@ramnode:/mnt/backup-Mail 2>&1 1>&3 | tr '\r' '\n' | grep -v ":Checking segments" 1>&2; } 3>&1

works for the first case, but not the second, namely just Remote:. I could use two greps, but I'd prefer one expression.


To filter out either message in one grep, pull in the -E extended regular expression flag (for the "zero or once" meaning of ? below), and the -v inverse match:

grep -Ev '^Remote:( Checking segments [[:digit:]]{1,3}\.[[:digit:]]%)?$'

which says that the lines should (inverse) match:

  • beginning of line ^
  • the string Remote:
  • the grouped ( ... ) set of characters (with a leading space) Checking segments ..., matched either zero or one times with: ?
  • the [[:digit:]] section says to match between 1 and 3 digit, followed by a period, followed by one digit, and then the % symbol
  • all anchored to the end of the line $

Upon further clarification from the OP, the final grep looks like:

grep -Ev "^Remote:\s*(Checking segments.*)?$"

Which removes any line matching (starting at the beginning ^ of the line), the word "Remote:", then any (or no) amount of whitespace, then either:

  • nothing, or

  • the phrase "Checking segments" itself followed by any (or no) characters .*

... and then the end of the line $

  • Hi Jeff, this gets blocked, which I don't want: echo "Remote: xx" | grep -Ev '^Remote:( Checking segments)?'. And what is the E flag required for? – Faheem Mitha May 1 '16 at 8:24
  • I extended the RE so that the two things it's looking for must match the entire line (anchored in '$'), and so I also had to fill in the percentage RE. – Jeff Schaller May 1 '16 at 11:38
  • It should suffice to just match Checking segments, and allow anything after it. Plus that would be simpler. – Faheem Mitha May 1 '16 at 12:18
  • Replace all that digit stuff with .* then, leaving the $ at the end to anchor – Jeff Schaller May 1 '16 at 14:29
  • So, what would that look like then? grep -Ev '^Remote:( Checking segments :.*)?$'? – Faheem Mitha May 1 '16 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.