0

I want to remove blank lines from text files. I'm simply trying to find a findstr command-line switch to find the solution but I can't get it to work. Here are my files:

test2.html:

<script src="common/latest_updates/angular-slider.js"></script>
<script src="common/latest_updates//angular-list-group.js"></script>
blank space
<script src="common/latest_updates/jstree/jstree.js"></script>
<script src="common/latest_updates/jstree/ngJsTree.js"></script>
blank space
<script src="common/d3/ngFunnel.js"></script>

Script:

findstr /r /v "^$" test2.html >> test3.html
Eric Aya
  • 69,473
  • 35
  • 181
  • 253

3 Answers3

3

I see two possible reasons why your FINDSTR command is not giving you the result you want.

  • The blank lines may not be empty, rather they may contain spaces and or tabs. Obviously your regex will not match a line containing whitespace.
  • The file may have unix format (lines end with <LF> instead of the Windows <CR><LF> standard). The $ anchor only matches the position before a <CR> so it can never match any line that uses unix line endings.

See What are the undocumented features and limitations of the Windows FINDSTR command? for more information.

I would use the following to match only lines that contain at least one non-white-space character. Note that <tab> represents a hard coded tab character, not the string literal.

findstr /rc:"[^ <tab>]" test2.html >test3.html

Entering a <tab> from the command line can be a bit of an issue if file completion is enabled in your console session. Pressing the [TAB] key will attempt to complete a file name. A way to get around the problem is to press and hold the [ALT] key and press the keyboard [9] key, and then release. A funny character may appear on your input line, but the correct <tab> value will be fed to FINDSTR.

If you use a batch script, then you should have no problem entering a <tab> character, unless your text editor converts tabs to spaces.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • This should be the accepted answer. There is no need to walk through every file line and check its contents. Walking through every line is usually done to change the contents within a line. The `findstr` command provides functionality that can discard entire lines of text from its search results whenever they meet a specific condition. I had totally overlooked this :> – 303 Jul 11 '17 at 00:28
1

This will walk through the source file test2.html and will only print the lines that are not empty. The result is written to the destination file test3.html.

@echo off

set "source=test2.html"
set "dest=test3.html"

1>"%dest%" (
  for /f "tokens=1,* delims=:" %%i in ('findstr /n "^" "%source%"') do (
    if not "%%j" == "" (
      echo;%%j
    )
  )
)

In order to process empty lines the same as lines that only contain spaces, will require a bit more fiddling but should be possible by toggling delayed expansion on and off at the right times.


Use the following code to overcome the problem that jeb mentioned, being able to process the line ::::: comment or /?. Thanks to dbenham's answer posted here.

@echo off

setlocal disabledelayedexpansion
set "source=test2.html"
set "dest=test3.html"

1>"%dest%" (
  for /f "delims=" %%e in ('findstr /n "^" "%source%"') do (
    set "line=%%e"
    setlocal enabledelayedexpansion
    set "line=!line:*:=!"

    if not "!line!" == "" (
      echo(!line!
    )
    endlocal
  )
)
303
  • 2,417
  • 1
  • 11
  • 25
  • 1
    Fails with lines in the source file, like `::::: comment` or `/?` – jeb Jul 10 '17 at 14:09
  • @jeb Thank you for your comment. Now I finally understand why `echo(` is preferred over `echo;`. I couldn't find more details about this on DosTips. – 303 Jul 10 '17 at 14:37
  • 2
    Description can be found at [ECHO. FAILS to give text or blank line - ...](http://www.dostips.com/forum/viewtopic.php?f=3&t=774) – jeb Jul 10 '17 at 14:42
0

This processes empty lines. Your post wasn't clear as to whether or not the indicated lines contained white space or not. It will get a bit more complicated if the indicated lines contain white space such as space or tab characters.

@echo off
if exist test3.html del test3.html
for /f "tokens=1*" %%a in (Test2.html) do (
    rem If adding a space to the line does not result in a line with only 1 space, output it.
    if not "%%a "==" " (
        echo %%a%%b>>test3.html
    )
)
start "test" notepad.exe test3.html
exit /b
thx1138v2
  • 566
  • 3
  • 6
  • 1
    `%%a` can't be empty, as FOR /f already skip in that case. Your solution fails for lines beginning with `;` and fails for lines like `ON` or `/?` – jeb Jul 10 '17 at 14:11
  • "...fails for lines beginning with ; and fails for lines like ON or /?" - none of which was in the original post or sample data. – thx1138v2 Jul 11 '17 at 00:07