Author Topic: Deleting all blank lines in a file  (Read 8865 times)

cdrdash

  • New Community Member
  • Posts: 1
  • Hero Points: 0
Deleting all blank lines in a file
« on: September 25, 2007, 06:41:36 PM »
Is there a simple way to delete all blank lines in a file with SlickEdit?

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: Deleting all blank lines in a file
« Reply #1 on: September 25, 2007, 07:19:42 PM »
Yes - e.g. by simply search'n replace them using a regex like that:
'c/^[ \t]*\n//@rp*' on SE cmdline.

HS2

Lee

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1299
  • Hero Points: 130
Re: Deleting all blank lines in a file
« Reply #2 on: September 26, 2007, 01:11:09 PM »
There is a ready-made command delete-blank-lines written for the Emacs emulation, but can be bound to a key in any emulation or used on the SlickEdit command-line any time.

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: Deleting all blank lines in a file
« Reply #3 on: September 26, 2007, 01:40:33 PM »
Good to know - that's much more convenient. Hopefully I'll remember next time I need it.
Although the regex method is more flexible (omit hidden lines, limit to selection etc.)...
Thanks Lee,
HS2

Lee

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1299
  • Hero Points: 130
Re: Deleting all blank lines in a file
« Reply #4 on: September 26, 2007, 01:57:09 PM »
Oops, just noticed the "all" in the Subject line.  delete-blank-lines only deletes all the blank lines from the current line.  It does not do the entire file, so the a regular expression search and replace is the right way to do the entire file.

bogedo

  • Community Member
  • Posts: 31
  • Hero Points: 0
Re: Deleting all blank lines in a file
« Reply #5 on: February 26, 2023, 07:02:51 PM »
aware that an old topic is being dug up but this is still relevant all these years later. hs2's solution still works but it would be great if we had a built in function to delete all the blank lines in a file.

Graeme

  • Senior Community Member
  • Posts: 2796
  • Hero Points: 347
Re: Deleting all blank lines in a file
« Reply #6 on: February 27, 2023, 05:50:13 AM »
If you expect to be able to reverse the operation in one step then it's not trivial unless you do it by saving a copy of the file before you do the delete.  I can't imagine anyone wanting to delete all the blank lines from a software source file  -  is that what you want this for or is it some other kind of file?

Tim Kemp

  • Senior Community Member
  • Posts: 546
  • Hero Points: 92
Re: Deleting all blank lines in a file
« Reply #7 on: March 04, 2023, 05:12:01 AM »
I've never needed to do it to a source code file, but I have done it regularly when I'm formatting data for a variety of things. Often the data ends up in a source code file.

mwb1100

  • Senior Community Member
  • Posts: 156
  • Hero Points: 13
Re: Deleting all blank lines in a file
« Reply #8 on: April 10, 2023, 06:39:26 PM »
A variation that I would use more often than deleting all blank lines is to "collapse" (or squeeze) blank lines (including those that have only whitespace characters) to a single blank line.  I'm pretty sure a regex to do this wouldn't qualify as simple.

Currently I use a simple bash function.  I guess I should figure out how to do this as a Slickedit macro:

Code: [Select]
squeeze ()
{
    sed 's/[ \t]*$//' "$1" | cat --squeeze-blank
}

Dan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2898
  • Hero Points: 153
Re: Deleting all blank lines in a file
« Reply #9 on: April 11, 2023, 05:34:07 PM »
From the SlickEdit command line, delete-search /^$/r would do it.  That's using SlickEdit regular expressions, convert to whatever kind you're the most comfortable with, allow for whitespace to be fancy if you wish.

https://youtu.be/P6WDBg4C2bY