error in tab/space with comma

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

error in tab/space with comma

#1 Post by pradeepcarya » 18 Oct 2010 06:21

hi ,
how can i replace tab/space with comma in a file(.csv file) by cmd commands?

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: error in tab/space with comma

#2 Post by ghostmachine4 » 18 Oct 2010 06:32

pradeepcarya wrote:hi ,
how can i replace tab/space with comma in a file(.csv file) by cmd commands?

sure, you can download gawk for windows or
sed for windows, then

Code: Select all

C:\test> sed -i.bak -r "s/[ \t]+/,/g" file
C:\test> gawk -F"[ \t]+" "{$1=$1}1" OFS="," file


If you want to replace 1 space/tab with 1 comma, then remove the "+" sign
Last edited by ghostmachine4 on 18 Oct 2010 07:41, edited 1 time in total.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: error in tab/space with comma

#3 Post by amel27 » 18 Oct 2010 06:48

via native CMD for TAB in file.csv:

Code: Select all

@(for /f "usebackq delims=" %%a in ("file.csv") do @(set "$a=%%a"
SetLocal EnableDelayedExpansion& echo !$a:   =,!
EndLocal))>"%~dpn0.tmp"
@copy /y "%~dpn0.tmp" "file.csv"&& del /f "%~dpn0.tmp"

P.S. replace spaces usually more complicated if dbl quotes used

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: error in tab/space with comma

#4 Post by ghostmachine4 » 18 Oct 2010 07:48

@amel27,

some tabs are 4 spaces, some are 8 , ie, its not fixed. The "tab" in your batch code is not the same as mine, therefore it doesn't work unless i change it in the code to the same number of tabs spaces. Any way to generalize it? like what i did for gawk using "\t" ?
also, what about spaces??

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: error in tab/space with comma

#5 Post by aGerman » 18 Oct 2010 11:49


Post Reply