[英]How can I use regular expression to search files in Unix?
I have following files from 2 different categories : Category 1 : MAA MAB MAC MAD MAE MAF MAG MAH MAJ MBA MBB MBC MBD MBE MDA MDD我有来自 2 个不同类别的以下文件: 类别 1:MAA MAB MAC MAD MAE MAF MAG MAH MAJ MBA MBB MBC MBD MBE MDA MDD
and Category 2 : MCA MCB MCC MCD MCE MCF MCG MDB和类别 2 : MCA MCB MCC MCD MCE MCF MCG MDB
So my question is : How can I write regular expression so that I can find files from category 1 only ?所以我的问题是:如何编写正则表达式,以便只能从类别 1 中找到文件?
I don't want to do hard coded script, expecting some logic from brilliant people.我不想做硬编码脚本,期待聪明人的一些逻辑。
I am trying this : find .我正在尝试这个:找到。 -regex "*[M][A,B,D][A,B,C,D,E,F,J].txt"
-regex "*[M][A,B,D][A,B,C,D,E,F,J].txt"
It's quite simple :这很简单:
ls -l | grep "MAA\|MAB\|MAC\|MAD\|MAE\|MAF\|MAG\|MAH\|MAJ\|MBA\|MBB\|MBC\|MBD MBE\|MDA\|MDD"
Ok so you don't want hardcoded.好的,所以你不想要硬编码。 Then yes you should state the patterns which should NOT match
-v
那么是的,您应该说明不匹配
-v
的模式
ls -l | grep -v "MC." | grep -v "pattern2" | ....
Your question is not very precise, but from your attempt, I conclude, that you are looking for files having names ending in ....MAA.txt, ...MAB.txt and so on, and being located in either your working directory or somewhere below.您的问题不是很精确,但是从您的尝试中,我得出结论,您正在寻找名称以 ....MAA.txt、...MAB.txt 等结尾的文件,并且位于您的工作目录中目录或下面的某个地方。
You also didn't mention, which shell you are using.您也没有提到您使用的是哪种外壳。 Here is an example using zsh - no need to write a regular expression here:
这是一个使用 zsh 的示例 - 无需在此处编写正则表达式:
ls ./**/*M{AA,AB,AC,AD,AE,AF,AG,AH,AJ,BA,BB,BC,BD,BE,DA,DD}.txt
I am trying this : find .
我正在尝试这个:找到。 -regex "*[M][A,B,D][A,B,C,D,E,F,J].txt"
-regex "*[M][A,B,D][A,B,C,D,E,F,J].txt"
The errors in this are:这其中的错误是:
.*
, unlike just *
in a normal filename pattern..*
,不像刚*
在一个正常的文件名模式。G
and H
in the third bracket expression.G
和H
MDB
.MDB
。 Besides:除了:
,
.,
分隔。[M]
) can be replaced by just the item ( M
).[M]
) 的括号表达式可以仅替换为项目 ( M
)。 This leads to:这将导致:
find . -regex ".*M[ABD].*" -not -name "MDB*"
or, without regex:或者,没有正则表达式:
find . -name "M[ABD]*" -not -name "MDB*"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.