find /v "" to get rid of blank lines

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gunitinug
Posts: 9
Joined: 09 Nov 2017 20:04

find /v "" to get rid of blank lines

#1 Post by gunitinug » 03 Dec 2017 17:10

Here's file.txt

Code: Select all

C:\Users\CMY\Desktop\test batch scripts\find v blank lines>type file.txt
aaa

bbb

ccc

ddd
I want to get rid of blank lines...

First of all,

Code: Select all

C:\Users\CMY\Desktop\test batch scripts\find v blank lines>type file.txt | findstr /v "^$"
aaa
bbb
ccc
ddd

this works.

But

Code: Select all

C:\Users\CMY\Desktop\test batch scripts\find v blank lines>type file.txt | find /v "^$"
aaa

bbb

ccc

ddd
and

Code: Select all

C:\Users\CMY\Desktop\test batch scripts\find v blank lines>type file.txt | find /v ""
aaa

bbb

ccc

ddd
doesn't work.... I want to do the same thing as findstr with find command.

THX

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: find /v "" to get rid of blank lines

#2 Post by penpen » 03 Dec 2017 17:59

gunitinug wrote:I want to do the same thing as findstr with find command.
If i don't miss anything, then your task is just impossible.

Is there any rerason for you not to use "findstr.exe"?


penpen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: find /v "" to get rid of blank lines

#3 Post by Squashman » 03 Dec 2017 18:59

Do you see a regular expression option in the help file for the FIND command?

gunitinug
Posts: 9
Joined: 09 Nov 2017 20:04

Re: find /v "" to get rid of blank lines

#4 Post by gunitinug » 03 Dec 2017 19:05

Squashman wrote:
03 Dec 2017 18:59
Do you see a regular expression option in the help file for the FIND command?
Yeh I googled a bit and find doesn't support regex.

But what about

Code: Select all

type file.txt | find /v ""
as an attempt to filter out blank lines?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: find /v "" to get rid of blank lines

#5 Post by Squashman » 03 Dec 2017 20:01

gunitinug wrote:
03 Dec 2017 19:05
Yeh I googled a bit and find doesn't support regex.
Why would it be so hard to open up a cmd prompt and actually read the help for the FIND command.

Code: Select all

Microsoft Windows [Version 10.0.15063]
(c) 2017 Microsoft Corporation. All rights reserved.

C:\Users\Squashman>find /?
Searches for a text string in a file or files.

FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]

  /V         Displays all lines NOT containing the specified string.
  /C         Displays only the count of lines containing the string.
  /N         Displays line numbers with the displayed lines.
  /I         Ignores the case of characters when searching for the string.
  /OFF[LINE] Do not skip files with offline attribute set.
  "string"   Specifies the text string to find.
  [drive:][path]filename
             Specifies a file or files to search.

If a path is not specified, FIND searches the text typed at the prompt
or piped from another command.

C:\Users\Squashman>

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: find /v "" to get rid of blank lines

#6 Post by penpen » 04 Dec 2017 03:36

gunitinug wrote:
03 Dec 2017 19:05
But what about

Code: Select all

type file.txt | find /v ""
as an attempt to filter out blank lines?
If there is an empty search string is given the seacrh result depends on the programmers choice and could be one of the following two cases:
1) No line matches.
This is the default (recommended) result for literal search algorithms, because on an empty search string is a meta character.
Using meta characters should be avoided by literal camparison tools, so an empty search string is interpreted as "i don't wanna search".
2) Every line matches.
If the programmer of a such a tool (like find.exe) want to allow searching for an empty string, then you should note that
every line contains an infinite amount of empty strings.
Let ε be defined as "" (empty string), then the following string examples are equal to each other:
- "abcde"
- "εabcde"
- "aεbcde"
- "abεcde"
- "abcεde"
- "abcdεe"
- "abcdeε"
- "εaεbεcεdεeε"
- "εεεεεεabcde"
- "abcdeεεεεεεεεεεεεε"
- ...

No matter how the programmer has implemented this detail, you won't get what you wanted.


penpen

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: find /v "" to get rid of blank lines

#7 Post by Aacini » 04 Dec 2017 07:27

A couple notes here:

The type file.txt | findstr /v "^$" form, using a pipe, requires to execute two copies of cmd.exe file (one for each side of the pipe). The findstr /v "^$" < file.txt form get the exact same result, but it is more efficient.

You may get rid of blank lines with a FOR command (with no need of FINDSTR nor FIND commands):

Code: Select all

for /F "delims=" %a in (file.txt) do echo %a
Antonio

Post Reply