A basic question of regex

Hi,

I have a lot of files which begin with the letter x:

xab
xac
xad
xae
...

I want to join all file with cat, i do

cat x* > file

My question is, by instinct i thank use

cat x.* > file

for join all file but don't work, why ?

I guess, you are assuming shell to interpret . and * as regular expression metacharacters, however shell supports glob at the point where you use it. * in glob has a different meaning then a * in regular expression.

1 Like

Doesn't look like there's a . in the filenames. Don't confuse regex (with . wildcard) and shell patterns (which do not accept . for a wildcard)

1 Like

I mean that:
The dot -- . -- matches any one character, except a newline
and after i put the * as regex...The asterisk -- * -- matches any number of repeats of the character string or RE preceding it
Then for me it would be work but not

What's your shell? Some shells understand regexes, others don't or do to a minor extent.

My shell is common is bash, my OS is OSX.
The dot work as dot in grep or sed or awk

Yes, that's grep and sed and awk, but not shell.

1 Like

A basic bourne shell doesn't understand regexes, only globs. There are no modifiers like * or +. only ranges [A-Z], and any single-character-match ?. Also, the glob must always match the entire filename, not just part of it as you would in a regex.

BASH does have regexes, but as an extension, not directly.

1 Like

If you want file names starting with x that have two or more letters you need:

$ cat x?* > file