Finding records, skipping records with all spaces

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Finding records, skipping records with all spaces

#1 Post by miskox » 06 Jan 2015 01:42

I have a file with some data:

IN_FILE.txt

Code: Select all

                                
some data some data some data some data1
some more data some data some data some data2
some data more moresome data some data some data3
                               
some data some data some data some data4
some data some data data some data some data5


I would like to remove all records with all spaces (different number of spaces).

I tried:

Code: Select all

findstr /E /V /C:" " in_file.txt>out_file.txt


It works... but I cannot guarantee not to have a record with a space at the end so wrong record could be lost (the same goes for a /B switch - cannot guarantee not to have a space at the beginning of a line).

This:

Code: Select all

findstr /B /E /V /C:" " in_file.txt>out_file.txt


does not work because empty records have more than one space.

One way could be to add more spaces (10 or more) because there would certanly be more than 10 spaces in 'empty' records. But I would prefer better solution.

At the moment I read complete file and skip 'empty' lines - it works but FINDSTR is much faster.

Can this be done with regular expressions (I am not a regular expression expert)?

Thanks.
Saso

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Finding records, skipping records with all spaces

#2 Post by foxidrive » 06 Jan 2015 02:00

The lines with spaces - do they have a space at the beginning or end too?

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Finding records, skipping records with all spaces

#3 Post by miskox » 06 Jan 2015 05:30

foxidrive wrote:The lines with spaces - do they have a space at the beginning or end too?


Lines with spaces have all spaces - nothing else. We can say that the number of spaces can be anything between 1 and n (n should be less than 200).

Thanks.
Saso

P.S. These are deleted records from my previous posts (for example viewtopic.php?p=38629#p38629)

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Finding records, skipping records with all spaces

#4 Post by dbenham » 06 Jan 2015 06:21

This is a simple regular expression issue that even FINDSTR can handle. It removes lines that contain nothing except 0 or more spaces from beginning to end. I'm assuming you want to remove completely empty lines as well.

Code: Select all

findstr /rv /c:"^ *$"  in_file.txt >out_file.txt

or

Code: Select all

findstr /rvx /c:" *" in_file.txt >out_file.txt


If you want to delete lines with only spaces, but preserve empty lines, then you just need one extra space:

Code: Select all

findstr /rv /c:"^  *$" in_file.txt >out_file.txt


Dave Benham

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Finding records, skipping records with all spaces

#5 Post by miskox » 06 Jan 2015 06:42

dbenham wrote:This is a simple regular expression issue that even FINDSTR can handle. It removes lines that contain nothing except 0 or more spaces from beginning to end. I'm assuming you want to remove completely empty lines as well.

Code: Select all

findstr /rv /c:"^ *$"  in_file.txt >out_file.txt

or

Code: Select all

findstr /rvx /c:" *" in_file.txt >out_file.txt


If you want to delete lines with only spaces, but preserve empty lines, then you just need one extra space:

Code: Select all

findstr /rv /c:"^  *$" in_file.txt >out_file.txt


Dave Benham


Yes, you are correct. Both empty lines and lines with spaces only (though there should be no way of empty lines to appear in a file in a first place).

Thanks. This solves my problem.

Somehow I understand first solution, I don't understand the second one:

Code: Select all

findstr /rvx /c:" *" in_file.txt >out_file.txt


How can /X and /V work together in one command?

Saso

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Finding records, skipping records with all spaces

#6 Post by dbenham » 06 Jan 2015 06:46

/V means skip any line that matches the search (invert the search result), regardless how simple or complicated the search. /X means the search must match the entire line.

So if the search matches any line that consists of nothing but 0 or more spaces, then the /V option will skip those lines.


Dave Benham

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Finding records, skipping records with all spaces

#7 Post by miskox » 06 Jan 2015 07:29

dbenham wrote:/V means skip any line that matches the search (invert the search result), regardless how simple or complicated the search. /X means the search must match the entire line.

So if the search matches any line that consists of nothing but 0 or more spaces, then the /V option will skip those lines.


Dave Benham


Thanks. (I have a localized version of XP so the translation might be confusing from the English original).

Saso

Post Reply