I'm dealing with a txt file that lists a bunch of other txt files, a lot of which I have wrongfully duplicated.

I'm on macOS so the lines listing the duplicated files all end with the pattern (NUMBER).txt. I want to delete all the lines that don't contain this pattern.

I came up with this command to test the regex but it does not work as it also prints the lines that end with (WORD).txt:

sed '/\(\d\)\.txt$|\(\d\d\)\.txt$|\(\d\d\d\)\.txt$/p' file.txt

I know I'm missing something but I can't figure out what... Thanks for your help!


1 Answer 1


This should work:

sed -E -n '/.*[0-9]+\.txt$/p' file.txt

If you take a look at info sed, at the paragraph 2.1 you will find some examples of how to use the combination sed -n '...p'.

The above command prints to stdout. If you want to modify the file in place add the -i option.

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.