2012-06-09 65 views
0

我寫了一個小腳本,它打印包含有問題的字符序列的文件的名稱。如何讓這個腳本更簡潔?

#!/bin/bash 
# Finds all files in the repository that contain 
# undesired characters or sequences of characters 

pushd .. >/dev/null 

# Find Windows newlines 
find . -type f | grep -v ".git/" | grep -v ".gitmodules" | grep -v "^./lib" | xargs grep -l $'\r' 

# Find tabs (should be spaces) 
find . -type f | grep -v ".git/" | grep -v ".gitmodules" | grep -v "^./lib" | xargs grep -l $'\t' 

# Find trailing spaces 
find . -type f | grep -v ".git/" | grep -v ".gitmodules" | grep -v "^./lib" | xargs grep -l " $" 

popd >/dev/null 

我想把它組合成一行,即通過讓grep尋找\ r OR \ t或尾隨空格。我將如何構造一個正則表達式來做到這一點?似乎對於轉義字符需要使用一個特殊的序列($'\X'),我不知道如何將這些組合起來...

我正在運行OS X,並且正在尋找一種適用於兩者的解決方案基於BSD和GNU的系統。

回答

1
find . -type f | grep -E -v ".git/|.gitmodules|^./lib" | xargs grep -E -l '$\r|$\t| $' 

不確定'$ \ r | $ \ t | $'將以這種方式引用,對我的系統進行簡單測試似乎可行。

我正在使用-E(擴展reg-exp)來grep,允許將多個搜索目標組合在一起。

較舊的Unix-en可能支持-E選項,因此如果出現錯誤消息標記,請將所有grep -E替換爲egrep

我希望這會有所幫助。

+0

看起來不錯,謝謝! –