3
April
2026
17:14

Regular expression to remove spaces at the end of lines

3 April 2026 17:14

You can use a regular expression to remove spaces at the end of lines.

Introduction

For one task I needed to remove spaces at the end of lines from text files

Removing spaces at the end of lines

Sample:     (?!\n\n)\s+$
Replacement:       \n


Example:

Before replacement:

a····
·······b···
·····c·
·····

d·····
eee

f


After replacement:

a
·······b
·····c
d
eee

f


This example and regular expression have been checked in the editor Mousepad-> Search -> Find and Replace (Ctrl+R).
Processing with sed does not work.



Related publications