C:\_G\WWW\~ELISANET\INFO\tscmd027.html
<http://www.elisanet.fi/tsalmi/info/tscmd027.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Fri 12-Oct-2018 02:41:12

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



27} How can I remove all the blank lines from a text file?

Assume e.g. the following LFN-type test file: "My test file.txt"
line 1
line 2 &()[]{}^=;!'+,`~
line 3 <>
line 4


line 7 Lines "5" and "6" are blank!
line 8
line 9 &()[]{}^=;!'+,`~

Several options are available. One pure script solution would be
  @echo off & setlocal enableextensions disabledelayedexpansion
  for %%f in ("myfileNoBlanks.txt") do if exist %%f del %%f
  for /f "delims=" %%r in ('type "My test file.txt"') do echo.%%r>>"myfileNoBlanks.txt"
  endlocal & goto :EOF

The contents of myfileNoBlanks.txt will be
line 1
line 2 &()[]{}^=;!'+,`~
line 3 <>
line 4
line 7 Lines "5" and "6" are blank!
line 8
line 9 &()[]{}^=;!'+,`~

SED Stream EDitor:
  @echo off & setlocal enableextensions
  set myfile_=My test file.txt
  for /f "tokens=* delims=" %%f in ("%myfile_%") do (
    set myfile_=%%~sf)
  sed "/^$/d" "%myfile_%"
  endlocal & goto :EOF
The renaming to a SFN-format is needed when the SED version usually assumed in this FAQ is used.

Using the native FINDSTR is equally easy. This regular expression solution would appear to be the best, natural option. Either from the command line or from within a script
  findstr /v "^$" "My test file.txt"

Incidentally, note the difference with the literal option
  findstr /v /c:"^$" "My test file.txt"
which, expectedly, will not solve the problem at hand.

Likewise, GAWK could be used
  @echo off & setlocal enableextensions
  <"My test file.txt" gawk '{if(length($0)!=0)printf"%%s\n",$0}'
  endlocal & goto :EOF
  @echo off & setlocal enableextensions
  <"My test file.txt" unxgawk "{if(length($0)!=0)printf\"%%s\n\",$0}"
  endlocal & goto :EOF
or
  @echo off & setlocal enableextensions
  set myfile_=My test file.txt
  for /f "tokens=* delims=" %%f in ("%myfile_%") do (
    set myfile_=%%~sf)
  gawk 'length()!=0' "%myfile_%"
  endlocal & goto :EOF
or (valid both on 32-bit and 64-bit)
  @echo off & setlocal enableextensions
  unxgawk "{if(length()>0)printf\"%%s\n\",$0}" "My test file.txt"
  endlocal & goto :EOF

If you wish to also define as an empty line a line that has nothing but blanks or tabs on it, then use
  @echo off & setlocal enableextensions
  <"My test file.txt" sed "/^[\x09 ]*$/d"
  endlocal & goto :EOF
or
  @echo off & setlocal enableextensions
  <"My test file.txt" gawk 'NF!=0'
  endlocal & goto :EOF
  @echo off & setlocal enableextensions
  <"My test file.txt" unxgawk "NF!=0"
  endlocal & goto :EOF

OK, but what if you wish to remove all the lines that are two characters or shorter. Or put in another way, you want to get all the lines that are at least 3 characters long. This is one way how to do it
  @echo off & setlocal enableextensions
  <"My test file.txt" gawk '{if(index(2-length(),"-"))printf"%%s\n",$0}'
  endlocal & goto :EOF
  @echo off & setlocal enableextensions
  <"My test file.txt" unxgawk "{if(index(2-length(),\"-\"))printf\"%%s\n\",$0}"
  endlocal & goto :EOF
or in abbreviated format
  @echo off & setlocal enableextensions
  <"My test file.txt" gawk 'index(2-length(),"-")'
  endlocal & goto :EOF
  @echo off & setlocal enableextensions
  <"My test file.txt" unxgawk "index(2-length(),\"-\")"
  endlocal & goto :EOF

Assume a slightly changed "My test file.txt"
line 1
line 2 &()[]{}^=;!'+,`~
line 3 <>

5
06
line 7 Line "4" is blank, "5" and "6" are short!
line 8
line 9 &()[]{}^=;!'+,`~
A pure command-line script solution is possible
  @echo off & setlocal enableextensions
  echo ^^$>"%temp%\findstr.tmp"
  echo ^^.$>>"%temp%\findstr.tmp"
  echo ^^..$>>"%temp%\findstr.tmp"
  findstr /v /g:"%temp%\findstr.tmp" "My test file.txt"
  for %%f in ("%temp%\findstr.tmp") do (
    if exist %%f del %%f)
  endlocal & goto :EOF

C:\_D\TEST>cmdfaq
line 1
line 2 &()[]{}^=;!'+,`~
line 3 <>
line 7 Line "4" is blank, "5" and "6" are short!
line 8
line 9 &()[]{}^=;!'+,`~

For demonstration, the problem can also be solved with a Visual Basic Script (VBScript) aided command line script. Use "Len(str) > 0" if the original task is being solved instead of the "two characters or shorter".
  @echo off & setlocal enableextensions
  ::
  :: Build a Visual Basic Script

  set skip=
  set vbs_="%temp%\tmp$$$.vbs"
  findstr "'%skip%VBS" "%~f0" > %vbs_%
  ::
  :: Run the script with Microsoft Windows Script Host Version 5.6

  cscript //nologo %vbs_% < "My test file.txt"
  ::
  :: Clean up

  for %%f in (%vbs_%) do if exist %%f del %%f
  endlocal & goto :EOF
  '
  '.......................................................
  'The Visual Basic Script
  '
  Do While Not WScript.StdIn.AtEndOfStream 'VBS
    str = WScript.StdIn.ReadLine 'VBS
    If Len(str) > 2 Then WScript.StdOut.WriteLine str 'VBS
  Loop 'VBS

References/Comments:

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd027.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd027
http://www.elisanet.fi/tsalmi/info/tscmd027.html
file:///c:/_g/www/~elisanet/info/tscmd027.html