カラーdiff出力をより少ない出力に転送するには?

カラーdiff出力をより少ない出力に転送するには?

私はgit diffを使っていて、カラー出力を生成します。しかし、特定のタスクを実行するには、通常のdiffを使用する必要があり、色の不足によって読みにくい出力がたくさん生成されることがわかりました。 diffに読みやすいカラー出力を生成させるにはどうすればよいですか?理想的には、大容量ファイルを見やすくするために、同時に少ない量で転送することをお勧めします。

ベストアンサー1

diff色を出力できません。これには、例えば別のプログラムが必要ですcolordiff。印刷による端末の色ANSIエスケープコードless はデフォルトでは解釈されません。less色を正しく表示するにはスイッチが必要-rです。-R

colordiff -- "$file1" "$file2" | less -R

からman less

   -R or --RAW-CONTROL-CHARS
          Like -r, but only ANSI  "color"  escape  sequences  are
          output in "raw" form.  Unlike -r, the screen appearance
          is maintained correctly in most  cases.   ANSI  "color"
          escape sequences are sequences of the form:

               ESC [ ... m

          where  the  "..."  is  zero or more color specification
          characters For the purpose of keeping track  of  screen
          appearance,  ANSI color escape sequences are assumed to
          not move the cursor.  You  can  make  less  think  that
          characters  other  than  "m"  can end ANSI color escape
          sequences by setting the environment  variable  LESSAN‐
          SIENDCHARS  to  the  list of characters which can end a
          color escape sequence.  And you  can  make  less  think
          that characters other than the standard ones may appear
          between the ESC and the m by  setting  the  environment
          variable  LESSANSIMIDCHARS  to  the  list of characters
          which can appear.

moreまたは、デフォルトで色を正しく表示するコマンドを使用できます。


外部プログラムをインストールできない場合は、より受動的なアプローチを使用して同じ出力を取得できます。

diff a b | 
   perl -lpe 'if(/^</){$_ = "\e[1;31m$_\e[0m"} 
              elsif(/^>/){$_ = "\e[1;34m$_\e[0m"}'

おすすめ記事