在bash中使用/bin/sort
我發現,輸出通常是不正確的排序,當它來自彩色輸入。Bash排序命令不正確排序彩色輸出
例如,在包含以下內容的目錄:
$ ls
dir1 (directory, printed in blue)
dir2 (directory, printed in blue)
dir3 (directory, printed in blue)
afile (file, printed in white)
file1 (file, printed in white)
file2 (file, printed in white)
file3 (file, printed in white)
我希望ls | sort
排序afile
,再dir1
,等等。相反,我得到:
$ ls | sort
dir2
dir3
dir1
afile
file1
file2
file3
我已經試過相當sort
(-d,-g,-h,-n)的一些選項無濟於事。
我已經能夠解決這個問題的唯一辦法是通過明確轉向ls
關閉的色彩輸出:
$ ls --color=never | sort
afile
dir1
dir2
dir3
file1
file2
file3
但是,這感覺就像一個變通辦法,而不是解決問題的辦法。我一直認爲必須有辦法在最終輸出中保留顏色,只適用於不關閉顏色的情況(例如對於不支持顏色禁用的ls
以外的命令)。
一種力如何排序以僅對打印字符(即文件和目錄名稱)起作用?我有興趣瞭解如何在事實之後徹底刪除顏色輸出(我已嘗試strings
,但獲取藍色文本的顏色說明符[01;34m
),並且特別關注是否可以在排序後保留顏色輸出。
當輸出到管道'ls'應該是默認禁用顏色。如果沒有發生這種情況,那意味着您可能不小心使用「--color = always」啓用了顏色,而不是僅使用「--color」。 – 2015-04-01 20:04:55
使用'\ ls | sort'爲'ls'禁用別名。 – Cyrus 2015-04-01 20:06:17
它應該足夠使用'--color = auto',對吧?當寫入管道時,它會抑制顏色,並且您不必擺弄這些選項。 [編輯:我看到@Etan也是這麼說的。] – alexis 2015-04-01 20:07:04