Page 1 of 1

use batch file to insert string "NULL" ..

Posted: 14 Jun 2012 03:14
by adeline
Basically i need to fill in some string on column that is NULL.. just insert NULL(string)

here is the scenario

input.txt

Code: Select all


field1      field2           field3
AAAA        BBBB             CCCC
DDDD                                 
EEEE        FFFF                 
GGGG                         HHHH     



Those empty one shall put a NULL inside
something like this,,

output.txt

Code: Select all


field1      field2           field3
AAAA        BBBB             CCCC
DDDD        NULL             NULL                           
EEEE        FFFF             NULL     
GGGG        NULL             HHHH     


## take note the field2 and field 3 space is different.. field 1 is almost with content hence the script just has to tackle field 2,3
My approach is to count the character until field 2 (12character from left) then check if is empty insert NULL. Then continue for field 3 (29 character from left) check if is empty insert null

Code: Select all

@echo off
set line =
for /F in(input.txt) do
if "!line :~ 12" eq " " do
write null     (i not sure whether is correct)
if "!line:~ 29 " eq " " do
write null   
echo  > output.txt


Would appreciate if anyone can guide me on this..
thanks

Re: use batch file to insert string "NULL" ..

Posted: 15 Jun 2012 01:07
by foxidrive
This assumes the strings are fixed lengths with every line padded with spaces.

Edited:

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /F "delims=" %%a in (input.txt) do (
set "line=%%a"
if "!line:~0,1!"==" " set "line=NULL!Line:~4!"
if "!line:~13,1!"==" " set "line=!line:~0,12!NULL!Line:~16!"
if "!line:~30,1!"==" " set "line=!line:~0,29!NULL"
echo >>"output.txt" !line!
)