Skip to content
Advertisement

Bash – Delete lines ending with a number

I need to remove only lines (in text file) ending with a number.

Before:

1987 Robocop
1990 Robocop 2 
1993 Robocop 3 
2014 Robocop

After:

1987 Robocop
2014 Robocop

Advertisement

Answer

Since you’re referencing my favorite movie, I’ll answer:

sed '/[ t][0-9][0-9]*[ t]*$/d' movies.txt

Which translates to:

Delete any line where there is either a space or a tab, followed by at least one digit, then any amount of spaces or tabs at the end of a line.

There are more compact ways to represent this on Linux, but I gave you one that does it the hard way for portability’s sake.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement