1

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!

h3lppp
  • 11

1 Answers1

0

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.

Francesco
  • 836