Observations suggest that star formation in filamentary molecular clouds occurs in a two-step process, with the formation of filaments preceding that of prestellar cores and stars. Here, we apply the gravo-turbulent fragmentation theory of Hennebelle & Chabrier (2008, 2009, 2013 to a filamentary environment, taking into account magnetic support. We discuss the induced geometrical effect on the cores, with a transition from 3D geometry at small scales to 1D at large ones. The model predicts the fragmentation behavior of a filament for a given mass per unit length (MpL) and level of magnetization. This CMF for individual filaments is then convolved with the distribution of filaments to obtain the final system CMF. The model yields two major results: (i) the filamentary geometry naturally induces a hierarchical fragmentation process, first into groups of cores, separated by a length equal to a few filament Jeans lengths, i.e. a few times the filament width. These groups then fragment into individual cores. (ii) Non-magnetized filaments with high MpL are found to fragment excessively, at odd with observations. This is resolved by taking into account the magnetic field treated simply as additional pressure support). The present theory suggests two complementary modes of star formation: while small (spherical or filamentary) structures will collapse directly into prestellar cores, according to the standard Hennebelle-Chabrier theory, the large (filamentary) ones, the dominant population according to observations, will follow the afore-described two-step process.