31

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)
0

5 Answers 5

44
  • 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

5
  • 5
    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, 2020 at 8:21
  • 1
    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, 2020 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, 2020 at 21:27
  • Does \R do both \r and \n characters?
    – Tim
    Jun 24, 2020 at 11:55
  • 2
    @Tim: \R stands for any kind of linebreak, \r, \n, \r\n
    – Toto
    Jun 24, 2020 at 12:23
18

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)

5
  • 3
    Just a remarck, \s contains also linebreaks, in this case it's better to use \h (horizontal spaces)
    – Toto
    Jun 21, 2020 at 16:04
  • 1
    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, 2020 at 16:08
  • @klepp0906 sure, happy to help.
    – Glorfindel
    Jun 21, 2020 at 16:13
  • 2
    A simple space will not work if there are some tabulations.
    – Toto
    Jun 21, 2020 at 16:17
  • 2
    @Toto correct; the OP says they have spaces so I assume it's not a problem for them.
    – Glorfindel
    Jun 21, 2020 at 19:06
4

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.

4
  • This will not remove End-Of-Line
    – Toto
    Jun 22, 2020 at 10:43
  • @Toto You could follow up with choosing "Extended" (instead of Regex) and finding "\n\n" and replacing with "\n" :) Jun 22, 2020 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, 2020 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. Jun 23, 2020 at 13:46
4

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.

2

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'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .