30

I need to remove lines that begin with spaces (the entire line, not just the spaces)

Here is an example of the file I'm working with:

41. 415607DB (AV-2011) 2007-06-23 (1D0703A2) (572D8DEC)
42. 415607DC (AV-2012) 2007-06-21 (4A5E805B) (73A29D08)
                       2009-11-18 (F830F241) (11F8A118)
43. 415607DD (AV-2013) 2007-07-12 (2D92F988) (2CB96AE4)
44. 415607DE (AV-2014) 2009-08-18 (A8E5F41B) (614BF7F6)
45. 415607DF (AV-2015) 2008-06-21 (8A3A88B8) (3B7CBB2D)
46. 415607E0 (AV-2016) 2011-03-29 (EF1BE81D) (2641EDA1)
                       2007-06-27 (7C19F25E) (1B17FCD8)
47. 415607E1 (AV-2017) 2007-11-21 (608D1D2F) (720B9353)
                       2007-11-29 (E55CEF10) (1BB2F934)
                       2007-06-21 (DD85E9F6) (2E07093A)
48. 415607E2 (AV-2018) 2009-07-07 (3D7B9FC0) (0F8AB402)

Here is the output I'm hoping to obtain:

41. 415607DB (AV-2011) 2007-06-23 (1D0703A2) (572D8DEC)
42. 415607DC (AV-2012) 2007-06-21 (4A5E805B) (73A29D08)
43. 415607DD (AV-2013) 2007-07-12 (2D92F988) (2CB96AE4)
44. 415607DE (AV-2014) 2009-08-18 (A8E5F41B) (614BF7F6)
45. 415607DF (AV-2015) 2008-06-21 (8A3A88B8) (3B7CBB2D)
46. 415607E0 (AV-2016) 2011-03-29 (EF1BE81D) (2641EDA1)
47. 415607E1 (AV-2017) 2007-11-21 (608D1D2F) (720B9353)
48. 415607E2 (AV-2018) 2009-07-07 (3D7B9FC0) (0F8AB402)
43
  • Ctrl+H
  • Find what: ^\h+.*$\R?
  • Replace with: LEAVE EMPTY
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

^           # beginning of line
  \h+       # 1 or more horizontal spaces
  .*        # 0 or more any character but newline
$           # end of line
  \R?       # optional linebreak

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

| improve this answer | |
  • 4
    NB: This will also delete lines that start with TABs. If you want to keep these lines, replace \h with a space. Also the + after \h isn't needed, but won't do much harm – CSM Jun 22 at 8:21
  • If you want, you can use the same regex in find and highlight the results to have a preview of what will be deleted. – bracco23 Jun 23 at 8:39
  • 1
    The \R trick is nice! I have previously been using this technique, but without that element, and had to follow-up with TextFX > TextFX Edit > Delete blank lines, afterwards. – HappyDog Jun 23 at 21:27
  • Does \R do both \r and \n characters? – Tim Jun 24 at 11:55
  • 1
    @Tim: \R stands for any kind of linebreak, \r, \n, \r\n – Toto Jun 24 at 12:23
16

It's possible with a pure regex solution (see @Toto's answer) but perhaps the following solution, using bookmarks, is easier to grasp:

  1. Open the Find dialog

  2. Go to the Mark tab, search for ^ (that's 2 characters: a caret indicating 'the beginning of the line' and a space) and enable the 'Bookmark line' option. I assume your Search Mode is already set to Regular expression. Hit the 'Mark All' button.

    enter image description here

  3. Delete all bookmarked lines (Search → Bookmarks → Remove bookmarked lines)

| improve this answer | |
  • 2
    Just a remarck, \s contains also linebreaks, in this case it's better to use \h (horizontal spaces) – Toto Jun 21 at 16:04
  • I selected Toto's answer as his answer made it in first. However tested this and is a viable alternative. Appreciate your time :) – klepp0906 Jun 21 at 16:08
  • @Toto thanks; even a space itself works. – Glorfindel Jun 21 at 16:13
  • 1
    A simple space will not work if there are some tabulations. – Toto Jun 21 at 16:17
  • 1
    @Toto correct; the OP says they have spaces so I assume it's not a problem for them. – Glorfindel Jun 21 at 19:06
3

You can do this:

  • Hit Ctrl+H for find and replace.

  • Check mark regular expression.

  • Use the Regex ^\s+.* in the Find box.

  • Keep Replace box blank.

  • Click Replace All.

| improve this answer | |
  • This will not remove End-Of-Line – Toto Jun 22 at 10:43
  • @Toto You could follow up with choosing "Extended" (instead of Regex) and finding "\n\n" and replacing with "\n" :) – Captain Man Jun 22 at 20:06
  • @CaptainMan: Sure, but 2 remarcks: 1) \n works only for Linux EOL. 2) Why doing a two-step process when it can be done in one step? – Toto Jun 23 at 10:05
  • @Toto I'm not saying it is perfect, just a way to fix it. To some people it may be easier to remember a simpler expression to delete the line like in this answer then remove empty lines. – Captain Man Jun 23 at 13:46
3

Another solution which would seem to apply to your example would be to Select All (Ctrl-A), then go to Edit>Line Operations>Sort Lines [pick one of them that best applies], and this will group all the lines beginning with spaces together in the sort, and you can then easily just select and delete them. This only works in this case because the lines you want to keep seem to be numerically sorted already. Obviously this might not apply if you don't want to change the sorting of the other lines.

Personally, I have Sort Lines Lexicographically Ascending and Sort Lines as Integers Ascending assigned to hotkeys because I use them so frequently for just this sort of thing.

| improve this answer | |
1

I know this a Notepad question, but I can’t resist.

In vim, you could do

:global/^\s/delete

Which says to delete all lines starting with space.

You can script for automation like

printf '%s\n' 'global/^\s/delete' 'write' 'quit' | vim -es --clean file

Alternatively, with sed if you need to process a stream:

sed '/^[[:space:]]/d'
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.