Here is an example based on the comment: ex -sc 'g/\v%2l|%4l/pu_' -cx alfa.txt Here is a working alternative to my original command: ex -sc '4pu_|2pu_|x' alfa.txt