如何使这个脚本更简洁?

时间:2012-06-09 03:33:56

标签: regex shell grep

我写了一个小脚本,打印出包含有问题的字符序列的文件的名称。

#!/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 \ n OR \ t或尾随空格。我如何构建一个正则表达式来做到这一点?对于转义字符来说,似乎需要使用一个特殊的序列($'\X'),我不确定如何将它们组合起来......

我正在运行OS X,正在寻找适用于基于BSD和GNU的系统的解决方案。

1 个答案:

答案 0 :(得分: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

我希望这会有所帮助。

相关问题