replacing tab/space with comma in a file via batch
Moderator: DosItHelp
-
- Posts: 4
- Joined: 18 Oct 2010 06:16
replacing tab/space with comma in a file via batch
hi guys,
I am very new for batch programming.
I am having a file mytext.dbf which contains tab/space delimited data.
I need to replace tab/space with comma via a batch file & o/p file sud have extension as mytext.csv(bcoz it contains comma separated values data)
kindly help me to do this task.
Can u suggest me any good batch programming doc also..
I am very new for batch programming.
I am having a file mytext.dbf which contains tab/space delimited data.
I need to replace tab/space with comma via a batch file & o/p file sud have extension as mytext.csv(bcoz it contains comma separated values data)
kindly help me to do this task.
Can u suggest me any good batch programming doc also..
Re: replacing tab/space with comma in a file via batch
I hate to bump an older thread, but is there an answer to this? I'm wondering the exact same thing.
Re: replacing tab/space with comma in a file via batch
The format of the .dbf files isn't mentioned but to replace space and tab with a comma, this will work:
This uses a helper batch file called repl.bat (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat in the same folder as the batch file or in a folder that is on the path.
Code: Select all
type file.csv | repl "(\t| )" "," x >newfile.csv
This uses a helper batch file called repl.bat (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat in the same folder as the batch file or in a folder that is on the path.
Re: replacing tab/space with comma in a file via batch
Thank you foxidrive. I knew there was a simple answer, but couldn't find it.
Re: replacing tab/space with comma in a file via batch
foxidrive wrote:The format of the .dbf files isn't mentioned but to replace space and tab with a comma, this will work:
Yes your code will work if this is a true text files that is nothing more than a delimited text file. Pretty sure this will not work for files in a DBF format but I could be wrong.
Re: replacing tab/space with comma in a file via batch
Squashman wrote:Pretty sure this will not work for files in a DBF format but I could be wrong.
You're right Squashman.
The question was a bit vague, so I concentrated on the bit that seemed important and was doable with an easy script.
Re: replacing tab/space with comma in a file via batch
Code: Select all
@echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%a in (input.txt) do (
set "line="
for %%b in (%%a) do set "line=!line!,%%b"
echo !line:~1!
)
Previous Batch file works as long as the file does not contain special Batch characters (like < > * ?). This may be fixed up to a certain degree...
Code: Select all
C:\> type input.txt
I am having a file mytext.dbf which contains tab/space delimite
d data.
I need to replace tab/space with comma via a batch file
C:\> test.bat
I,am,having,a,file,mytext.dbf,which,contains,tab/space,delimited,data.
I,need,to,replace,tab/space,with,comma,via,a,batch,file
Antonio
Re: replacing tab/space with comma in a file via batch
It may not matter for the OP, but two tabs in a row could delimit two separate records, and this will merge the empty fields Antonio.
Re: replacing tab/space with comma in a file via batch
I just tried this and it treats spaces like tabs as well. Any ideas on how to get it work with only tab?Aacini wrote:Code: Select all
@echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%a in (input.txt) do (
set "line="
for %%b in (%%a) do set "line=!line!,%%b"
echo !line:~1!
)
Re: replacing tab/space with comma in a file via batch
I found a solution here that works: http://stackoverflow.com/questions/1096 ... -delimited
This worked on my file that also had words with spaces between them in the fields separated by tabs. Hope this helps someone else.
Code: Select all
setlocal disableDelayedExpansion
set input="Customer_Tab.pwf"
set output="Customer.csv"
::There should be a TAB character after the equal below
set "tab= "
>%output% (
for /f "delims=" %%A in ('findstr /rn "^" %input%') do (
set ln=%%A
setlocal enableDelayedExpansion
set "ln=!ln:*:=!"
if defined ln set "ln=!ln:%tab%=,!"
echo(!ln!
endlocal
)
)
This worked on my file that also had words with spaces between them in the fields separated by tabs. Hope this helps someone else.