Page 1 of 1

Finding records, skipping records with all spaces

Posted: 06 Jan 2015 01:42
by miskox
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

Re: Finding records, skipping records with all spaces

Posted: 06 Jan 2015 02:00
by foxidrive
The lines with spaces - do they have a space at the beginning or end too?

Re: Finding records, skipping records with all spaces

Posted: 06 Jan 2015 05:30
by miskox
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)

Re: Finding records, skipping records with all spaces

Posted: 06 Jan 2015 06:21
by dbenham
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

Re: Finding records, skipping records with all spaces

Posted: 06 Jan 2015 06:42
by miskox
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

Re: Finding records, skipping records with all spaces

Posted: 06 Jan 2015 06:46
by dbenham
/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

Re: Finding records, skipping records with all spaces

Posted: 06 Jan 2015 07:29
by miskox
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