Solved Find rows having null by position using batch script

April 17, 2019 at 06:04:44
Specs: DOS
I've data as below

Apple,Fruit,Fiber
Bacon,,Protein
Avocado,,Fat

My script should print 2nd and 3rd lines because the second column is empty.
The script should allow the column number (in this case 2) as parameter and dynamically achieve this.

Thanks in advance for help!



See More: Find rows having null by position using batch script

Report •

✔ Best Answer
April 27, 2019 at 21:43:19
Old waters under the bridge...
@echo off & setlocal
set target=2
for /f "tokens=*" %%a in (test2) do call :xx "%%a"
goto :eof

:xx
set k=%1
set z=%1
set k=%k:,=","%
rem echo %k%
set c=1
call :yy %k% %z%
goto :eof

:yy
if %1 equ "" echo %c%::== blank: %z%
if %c% equ %target% echo ============== %z:~1,-1%
shift
set /a c+=1
set a=%1
if not defined a goto :eof
goto :yy

message edited by nbrane



#1
April 17, 2019 at 06:32:42
im gonna do it the incorrect way ;) 5min

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@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded


Report •

#2
April 17, 2019 at 06:45:24
given that input.txt contains:

Apple,Fruit,Fiber
Bacon,,Protein
Avocado,,Fat

tets.bat:

@echo off
for /f "tokens=1-3 delims=," %%a in (input.txt) do if "%%c"=="" echo %%a,,%%b
pause

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@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded

message edited by hidde663


Report •

#3
April 17, 2019 at 07:53:37
Thanks for the reply.

I've a small problem here. If a record is having more than 3 columns and 2nd column is empty, that column is not printed.

Apple,Fruit,Fiber
Bacon,,Protein
Avocado,,Fat
Cake,,Sugar,Cream

I'm expecting 4 row to be printed. Could you please help with this.

Thank you for the great help.


Report •

Related Solutions

#4
April 17, 2019 at 08:16:33
well then i'll have to do it the correct way :)

open your input file in notepad & press ctrl + h (replace), and replace all ,, with , ,
if not possible it will cost me quite a lot of time to manually look for double comma's and fill in the gaps.

how many columns are we dealing with (mamimum number of columns)?
are there any missing columns i have to deal with? (as in some are only lenght 4, some 5 or more)

a full example, with no missing info would be sweet.(including the ,, replacement with , ,)

if you are replacing sensitive words with random words, just use random letters and numbers as replacements to make it easier for yourself.(excluding: , )

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@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded


Report •

#5
April 17, 2019 at 08:51:32
This is yet another case where it's just easier in PowerShell.
gc input.txt | ? { [string]::IsNullOrWhiteSpace(($_ -split ',')[1]) }

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#6
April 17, 2019 at 09:58:45
yup, but am considering F# at the moment.
Will probably just start learning both tho.

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@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded


Report •

#7
April 17, 2019 at 10:32:19
While I'm glad you're moving on to "real" programming languages, there's different use cases for both. For one, F#'s a compiled language whereas PowerShell is a Win10 scripting environment. That said, learning both shouldn't be too much of an issue since they're both .NET based.

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#8
April 25, 2019 at 05:42:45
I cannot replace manually. It should be an automated process. Could you please help me with the correct way :).

I tried to do few things but nothing worked.

Sample data is as below.

hello,world,how,are,you
hello,,how,are,you
hello,,how,are,you,hello
hello,,how,are

I'm looking for second column to have a valid data. So it should give me 2nd,3rd and 4th rows as error rows. The length of the row is not fixed here.

Thanks a lot hidde663 for your help :)


Report •

#9
April 27, 2019 at 21:43:19
✔ Best Answer
Old waters under the bridge...
@echo off & setlocal
set target=2
for /f "tokens=*" %%a in (test2) do call :xx "%%a"
goto :eof

:xx
set k=%1
set z=%1
set k=%k:,=","%
rem echo %k%
set c=1
call :yy %k% %z%
goto :eof

:yy
if %1 equ "" echo %c%::== blank: %z%
if %c% equ %target% echo ============== %z:~1,-1%
shift
set /a c+=1
set a=%1
if not defined a goto :eof
goto :yy

message edited by nbrane


Report •

Ask Question