1

Why is it that when I pipe svn diff output to, say less, I get a bunch of ESC characters?

$ svn diff | less

Index: test/unit/tour_guide_tip_test.rb
===================================================================
ESC[1;31m--- test/unit/tour_guide_tip_test.rb   (revision 0)ESC[0;0m
ESC[1;34m+++ test/unit/tour_guide_tip_test.rb   (revision 66)ESC[0;0m
ESC[1;35m@@ -0,0 +1,7 @@ESC[0;0m
ESC[1;34m+require 'test_helper'ESC[0;0m
ESC[1;34m+ESC[0;0m
ESC[1;34m+class TourGuideTipTest < ActiveSupport::TestCaseESC[0;0m
ESC[1;34m+  # test "the truth" doESC[0;0m
ESC[1;34m+  #   assert trueESC[0;0m
ESC[1;34m+  # endESC[0;0m
ESC[1;34m+endESC[0;0m

However, if I direct output to a file (svn diff > whatever.diff) and then less whatever.diff it looks fine?

$ svn diff > whatever.diff
$ less whatever.diff

Index: test/unit/tour_guide_tip_test.rb
===================================================================
--- test/unit/tour_guide_tip_test.rb    (revision 0)
+++ test/unit/tour_guide_tip_test.rb    (revision 66)
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class TourGuideTipTest < ActiveSupport::TestCase
+  # test "the truth" do
+  #   assert true
+  # end
+end

1 Answers1

2

These escape characters mark parts of the output that are colored if they are not piped.

Based on this very similar question and its answer I suggest you try to use less -R instead of plain less.

If you want to use other tools than just less, you could strip the sequences with sed 's/\x1b\[[0-9]*m//g' as discussed in this answer.

Your complete command would then be

svn diff | sed 's/\x1b\[[0-9]*m//g' | othertool

In OS X escape codes are different so the pattern must be changed. It also seems to be the case that (using OS X) the escape sequence \x1b will work with perl but not with sed. A resulting expression that works for both linux and OS X is then:

svn diff | perl -pe "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mK]//g" | othertool
Tim
  • 2,202