简体   繁体   English

如何使用正则表达式在 Unix 中搜索文件?

[英]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:这其中的错误是:

  • The wildcard for any characters in a regex is .* , unlike just * in a normal filename pattern.对于一个正则表达式的任何字符,通配符.* ,不像刚*在一个正常的文件名模式。
  • You forgot G and H in the third bracket expression.您忘记了第三个括号表达式中的GH
  • You didn't exclude the category 2 name MDB .您没有排除类别 2 名称MDB

Besides:除了:

  • The characters of a bracket expression are not to be separated by , .括号表达式的字符不能用,分隔。
  • A bracket expression with a single item ( [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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM