Page 1 of 1
replacing tab/space with comma in a file via batch
Posted: 19 Oct 2010 00:22
by pradeepcarya
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..
Re: replacing tab/space with comma in a file via batch
Posted: 17 Jun 2014 08:25
by Samir
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
Posted: 17 Jun 2014 08:36
by foxidrive
The format of the .dbf files isn't mentioned but to replace space and tab with a comma, this will work:
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.batPlace 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
Posted: 17 Jun 2014 08:49
by Samir
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
Posted: 17 Jun 2014 10:13
by Squashman
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
Posted: 17 Jun 2014 10:59
by foxidrive
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
Posted: 17 Jun 2014 14:38
by Aacini
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
Posted: 18 Jun 2014 02:40
by foxidrive
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
Posted: 27 Aug 2014 21:16
by Samir
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!
)
I just tried this and it treats spaces like tabs as well. Any ideas on how to get it work with
only tab?
Re: replacing tab/space with comma in a file via batch
Posted: 27 Aug 2014 21:50
by Samir
I found a solution here that works:
http://stackoverflow.com/questions/1096 ... -delimitedCode: 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.