简体   繁体   中英

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

and Category 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 ?

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"

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

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.

You also didn't mention, which shell you are using. Here is an example using zsh - no need to write a regular expression here:

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"

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.
  • You didn't exclude the category 2 name 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 ).

This leads to:

find . -regex ".*M[ABD].*" -not -name "MDB*"

or, without regex:

find . -name "M[ABD]*" -not -name "MDB*"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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