replacing tab/space with comma in a file via batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pradeepcarya
Posts: 4
Joined: 18 Oct 2010 06:16

replacing tab/space with comma in a file via batch

#1 Post by pradeepcarya » 19 Oct 2010 00:22

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..

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: replacing tab/space with comma in a file via batch

#2 Post by Samir » 17 Jun 2014 08:25

I hate to bump an older thread, but is there an answer to this? I'm wondering the exact same thing.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: replacing tab/space with comma in a file via batch

#3 Post by foxidrive » 17 Jun 2014 08:36

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.bat

Place repl.bat in the same folder as the batch file or in a folder that is on the path.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: replacing tab/space with comma in a file via batch

#4 Post by Samir » 17 Jun 2014 08:49

Thank you foxidrive. 8) I knew there was a simple answer, but couldn't find it.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: replacing tab/space with comma in a file via batch

#5 Post by Squashman » 17 Jun 2014 10:13

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: replacing tab/space with comma in a file via batch

#6 Post by foxidrive » 17 Jun 2014 10:59

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. :)

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: replacing tab/space with comma in a file via batch

#7 Post by Aacini » 17 Jun 2014 14:38

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: replacing tab/space with comma in a file via batch

#8 Post by foxidrive » 18 Jun 2014 02:40

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.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: replacing tab/space with comma in a file via batch

#9 Post by Samir » 27 Aug 2014 21:16

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?

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: replacing tab/space with comma in a file via batch

#10 Post by Samir » 27 Aug 2014 21:50

I found a solution here that works: http://stackoverflow.com/questions/1096 ... -delimited

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. 8) Hope this helps someone else.

Post Reply