-2

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"

Mohan Ahire
  • 214
  • 2
  • 9
  • It's not unix forum; try grep function or http://stackoverflow.com/questions/6844785/how-to-use-regex-with-find-command –  Dec 23 '15 at 08:45
  • what kind of logic you expect? regex comes from pattern. You have to provide information about the pattern you are deciding to separate your categories on. As an observation, if your 'MDB' in category 2 was supposed to be part of category 1 than you can create a logic based on `MA*` `MB*` `MD*` for category 1 and `MC*` for category 2;) – Hemang Dec 23 '15 at 08:53
  • can we separate it out after occurrence of "D" at 2nd place ? – Mohan Ahire Dec 23 '15 at 09:00
  • 1
    There is no simple regexp for this as you have MDA MDD in the first category and MDB in the second (this looks much like exceptions than regexp capture). Could you specify what defines the first and second categories in a better clear way? – Jean-Baptiste Yunès Dec 23 '15 at 10:25

3 Answers3

0

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" | .... 
victor
  • 1,626
  • 1
  • 14
  • 23
0

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
user1934428
  • 19,864
  • 7
  • 42
  • 87
0

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*"
Armali
  • 18,255
  • 14
  • 57
  • 171