I would like to use batch file to insert a string to replace the empty space in particular column
say i have a input.txt like following
field1 field2 field3
AAAAA BBBBB CCCCC
DDDDD EEEEE
FFFFF
GGGGG HHHHH
I need to insert a string “NULL” on each field that is empty and for sure that field 1 is not empty and field 2,3 sometime will be empty. In addition, space between field1 &field2 is different from field2 & field 3
output.txt
field1 field2 field3
AAAAA BBBBB CCCCC
DDDDD NULL EEEEE
FFFFF NULL NULL
GGGGG HHHHH NULL
Because i am still need to batch file scripting..
i try the to write the code (field 2 is always start from 12 character from left and field 3 is always 29 character from left)
@echo off
set line=
for /F in (input.txt)do
if "!line:~12" equ " "
write "NULL" >> (i am not sure whether this work)
if "!line:~29" equ " "
write "NULL"
echo .>> output.txt
Perhaps, anyone could correct my mistake?
thanks!!
As promised, here is a solution in Python. This program will work just fine with either Python 3.x or with Python 2.7. If you are very new to programming, I suggest Python 3.x, because I think it is easier to learn. You can get Python for free from here: http://python.org/download/
The latest version of Python is version 3.2.3; I suggest you get that.
Save the Python code in a file called
add_null.pyand run it with the command:The code, with lots of comments:
Output from running this program: