Shell script regex help: accept only 3 file extensions

This regex is supposed to accept files with extensions 270, 276, and "txt" only. Everything else should be discarded.

This is what I have. I'll spare you the rest of the code.

[..]
 ext =".[(27(0|6).txt)]\$"
 #ext =".[(27|0|6|.txt)]\$"
 #ext =".[27|0|6|.|txt]\$"
 #ext =".[27(0|6)|.txt]\$"

[..]
   for xfile in `ls $dir | grep "$ext" | xargs`; do
 [..]
 

The problem with my regex is that is accepting more than those 3 file extensions.

Any help is appreciated.

This type of construct works for me, as an example:

me@www:/tmp# ls | egrep '27(0|6)\.txt$'
270.txt
276.txt

Edit: Nevermind... seems the poster wants only extensions \.270$ \.276$ and \.txt$ and not .*270\.txt etc... thanks Scrutinizer

Hi,

egrep "\.(27[06]|txt)$"

should work

thnx guys, I appreciate it.

now for one of the if-statements I have down the code...

what type of construct would help me get only these 4 file ext variants?
*.270
*.276
*.270.txt
*.276.txt

I'm trying this regex

ext="27[(0|6)|.txt]\$"; 
#ext = "(27[(0|6)]) | [.txt]\$"; 

but for some reason it doesn't accept:
270.txt
276.txt

egrep "\.27[06](|\.txt)$"

-or-

egrep "\.27[06](\.txt){0,1}$"

should do the trick...

right on!! thnx, that one did it. It works like a charm.

for xfile in $dir/*.27{0,6}{,.txt}; do
...

don't use ls to feed a for loop

It works for ksh/bash/zsh though not for plain posix:

$ for x in *.27[06]{,.txt}; do ls -l $x; done
ls: cannot access *.27[06]{,.txt}: No such file or directory

fretting too much on regex is bad for your health if you are just going to get 3 types

for file in *.27[06] *.txt
do
  echo $file
done

Sure, it's just that the OP wants to restrict it to only these four extensions: *.270 *.276 *.270.txt *.276.txt. :wink: