Plasma-based accelerators offer the possibility to drive future compact light sources and high-energy physics applications. Achieving good beam quality, especially a small beam energy spread, is still one of the major challenges. Here, we propose to use a periodically modulated plasma density to shape the longitudinal fields acting on an electron bunch in the linear wakefield regime. With simulations, we demonstrate an on-average flat accelerating field that maintains a small beam energy spread.