Home > database >  Regex to remove blank lines at the end of a CSV
Regex to remove blank lines at the end of a CSV

Time:09-22

I have the following CSV data:

1,2,3,4,5,6,7,8,9,10
,,,,,,,,,
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,

And I'm using the the following regex pattern to remove blank lines at the end of the CSV: [,\s] $

The issue is that this captures the empty columns in the fourth line too (1,2,3,4,5,,,,). How can I avoid this?

After removing the match, the desired output should be the following:

1,2,3,4,5,6,7,8,9,10
,,,,,,,,,
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,,,,,

CodePudding user response:

One way to do this is to look for only empty lines at the end of the file and remove them all:

(\n,*) $/g

An example can be found here, it just requires using an empty substitution as demonstrated.

  • Related