4

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 ]

3

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:

  • Thanks for your quick and competent answer. My real problem was an old SuSE (10.1) version, which does not work with regex in magic file. I just tried on SuSE 12.1 (also old) and it works. – Horst Jul 27 '16 at 5:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.