How can I read a number with file (magic)

file-commandfile-types

I would like to write the format of a drawing with "file drawing"

Files start like:

CAD
A1 
mm

(Blank after A1)

or:

CAD
A00
m 

(Blank after "m")

I tried somethings like in the file magic:

0      string  CAD\n     CAD-Drawing
>&0    regex   ^A[0-9]+  Format=[%s]
>>&0   search  \n
>>>&0  regex   ^[a-z]+   Units=[%s]

but with no luck!
Is there any way to solve this problem?
I would prefer to get no blanks.

That means not just:

0     string  CAD\n  CAD-Drawing
>&0   string  x      Format=[%s]
>>&0  string  x      Units=[%s]

which results in … Format=[A1 ] … or … Units=[m ]

Best Answer

Regexes in man magic are not extensively detailed. But have a look at this brilliant answer by JigglyNaga.

For a start you need to escape several characters in regexes in magic files: ^, + and spaces are examples. Here are two ways of making your magic file work for the files you describe:

0      string  CAD\n       CAD-Drawing
>&0    regex   \^A[0-9]\+  Format=[%s]
>>&0   search  \n
>>>&0  regex   \^[a-z]\+   Units=[%s]

This ignores the spaces, and therefore will print the following:

$ file -m mmm file[12].cad 
file1.cad: CAD-Drawing Format=[A1] Units=[mm]
file2.cad: CAD-Drawing Format=[A00] Units=[m]

A better way (in my humble opinion) is to keep spaces in the Version and Units strings:

0      string  CAD\n         CAD-Drawing
>&0    regex   \^A[0-9\ ]\+  Format=[%s]
>>&0   search  \n
>>>&0  regex   \^[a-z\ ]\+   Units=[%s]

(Note that I needed to escape the spaces even that they're inside character groups).

This prints the following:

$ file -m mmm file[12].cad 
file1.cad: CAD-Drawing Format=[A1 ] Units=[mm]
file2.cad: CAD-Drawing Format=[A00] Units=[m ]

References:

Related Question