delete blank lines and line with only commas

May 21, 2019 at 03:21:42
Specs: DOS
This code is working fine except for one case.

when the data has spaces its deleting the entire data after space

eg:
Input
name,company,last name,salary

,,,,,
jon,GOT,Snow,100

Output
name,company,last
jon,GOT,Snow,100

for /f %%a in (%infile%) do call :xx %%a
goto :eof
:xx
:: just debugging dislay FYI
echo 1: %1
echo all: %*
if "%1" neq "" (
>>%outfile1%: echo %*
) else (
>>%errfile% echo %*
)

goto :eof


See More: delete blank lines and line with only commas

Reply ↓  Report •

#1
May 21, 2019 at 05:36:09
i think it's because "for /f %%a" uses by default:

for /f "tokens=1 delimns= " %%a

to clarify for delims: SPACE + TAB

try changing it to(did not test it btw.):

for /f "tokens=1* delims=" %%a in (%infile%) do call :xx "%%a"
goto :eof
:xx
:: just debugging dislay FYI
echo 1: %~1
echo all: %~*
if "%~1" neq "" (
>>%outfile1%: echo %~*
) else (
>>%errfile% echo %~*
)
goto :eof

i5-6600K[delid]@4.9GHz/4.4GHz cache@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1450Mhz core@0.925v/2250MHz@1500MHz timings BiosModded


Reply ↓  Report •

#2
May 22, 2019 at 01:54:07
For some strange reason I'm getting blank file.

Even the original script posted in the question is behaving the same way.

How can this even happen. It was running fine yesterday.

Any thoughts?

If we can at least delete lines with only commas.. that will work for me.

Thanks!!


Reply ↓  Report •

#3
May 22, 2019 at 03:15:05
if %infile% is not defined, you should get no output, as it would not have a file to read.
which in your script is not defined, so my quess is that you set inflie from the commandline, then ran the script

anyways, what is the goal, what output do you want?

also this:

@echo off
set infile=input.txt
for /f "tokens=1-4 delims=," %%a in (%infile%) do echo %%a,%%b,%%c,%%d

gives me this output:

name,company,last name,salary
jon,GOT,Snow,100

i5-6600K[delid]@4.9GHz/4.4GHz cache@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1450Mhz core@0.925v/2250MHz@1500MHz timings BiosModded


Reply ↓  Report •

Related Solutions

#4
May 22, 2019 at 03:24:15
%infile% is defined

For this input
header: name,company,last name,salary
row1:
row2: ,,,,,
row3: jon,GOT,Snow,100

I need output

name,company,last name,salary
jon,GOT,Snow,100


With for tokens delims.. row2 will be echoed I donot want the commas row

Thanks!!


Reply ↓  Report •

#5
May 22, 2019 at 04:09:39
it is not echoed in #3, just look at my output

add >>%outfile1% behind %%d to echo it to the file

i5-6600K[delid]@4.9GHz/4.4GHz cache@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1450Mhz core@0.925v/2250MHz@1500MHz timings BiosModded


Reply ↓  Report •

#6
May 22, 2019 at 17:44:04
Also, might try adding 'find /v ,,,,,' to the for-loop basis:
for /f "tokens=1-4 delims=," %%a in ('find /v ",,,,,"^<%infile%') do echo %%a,%%b,%%c,%%d

that should exclude the "null-comma" lines even before the batchscript has to try to deal with them.


Reply ↓  Report •

#7
May 23, 2019 at 02:19:32
Thanks nbrane for the reply.

The number of commas are not fixed. Script should be able to check if the entire row is of commas and discard that record.

Thank you!


Reply ↓  Report •

#8
May 23, 2019 at 18:33:17
much as I hate using FINDSTR, maybe it will actually work here (limited tests proved ok, recommend checking all output until confident of results):
for /f "tokens=1-4 delims=," %%a in ('findstr /v "^,*$"^<%infile%') do echo %%a,%%b,%%c,%%d

Reply ↓  Report •

#9
May 29, 2019 at 05:35:03
RE #8

I really appreciate the reply @nbrane

I tried executing the script it runs indefinitely and I'm not getting any output. Am I missing something?

@echo off
SETLOCAL EnableDelayedExpansion

SET infile=***Actual file path*******
SET outfile=***Actual file path*******

for /f "tokens=1-4 delims=," %%a in ('findstr /v "^,*$"^<%infile%') do echo %%a,%%b,%%c,%%d >> %outfile%


Reply ↓  Report •

#10
May 29, 2019 at 21:54:11
I'm not sure the FOR loop is even required unless you need to restrict the output. Maybe try this and see, just for now make sure the input-file is right where you run this script from and it's just named "test":
findstr /v "^,*$" test
copy your input-file to "test", in the same directory as this "script" (it's actually only a command at this point") and type in or run the command, and check the output. If the screen stays blank, hit a couple of ENTER keys. If the cursor goes down, that means findstr is trying to read from stdin (console/keyboard) and not a file, which means you've messed up somewhere. I did re-check the findstr pattern and it seemed to work.
And final note make sure your input-file is not Unicode.

message edited by nbrane


Reply ↓  Report •

Ask Question