我的 LIST 文件中有很多行,只想列出名称不以(或包含)“git”开头的行。
到目前为止我有:
cat LIST | grep ^[^g]
Run Code Online (Sandbox Code Playgroud)
但我想要类似的东西:
#not starting by "git"
cat LIST | grep ^[^(git)]
#not containing "git"
cat LIST | grep .*[^(git)].*
Run Code Online (Sandbox Code Playgroud)
但这是不正确的。我应该使用什么正则表达式?
hwn*_*wnd 20
使用grep在这种情况下与-P选项,它解释图案作为一个Perl的正则表达式
grep -P '^(?:(?!git).)*$' LIST
Run Code Online (Sandbox Code Playgroud)
正则表达式解释:
^ the beginning of the string
(?: group, but do not capture (0 or more times)
(?! look ahead to see if there is not:
git 'git'
) end of look-ahead
. any character except \n
)* end of grouping
$ before an optional \n, and the end of the string
Run Code Online (Sandbox Code Playgroud)
使用find命令
find . \! -iname "git*"
Run Code Online (Sandbox Code Playgroud)
wis*_*cky 12
由于 OP 正在寻找通用正则表达式而不是专门用于 grep,因此这是不以“git”开头的行的通用正则表达式。
^(?!git).*
分解:
^ 行首
(?!git) 后面没有“git”
.* 后跟 0 个或多个字符
| 归档时间: |
|
| 查看次数: |
86693 次 |
| 最近记录: |