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
  • See [Why does my regular expression work in X but not in Y?](https://unix.stackexchange.com/questions/119905/why-does-my-regular-expression-work-in-x-but-not-in-y) - it will answer several issues like which metacharacters should be escaped, `\d` not supported, etc – Sundeep Jul 04 '20 at 09:55
  • `\d` is not (traditional) `sed` regex syntax; instead it's just a `d`; did you try to read the sed manual page? – U. Windl Oct 08 '21 at 07:55

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
  • 808
  • 7
  • 24