Page 1 of 1

error in tab/space with comma

Posted: 18 Oct 2010 06:21
by pradeepcarya
hi ,
how can i replace tab/space with comma in a file(.csv file) by cmd commands?

Re: error in tab/space with comma

Posted: 18 Oct 2010 06:32
by ghostmachine4
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

Re: error in tab/space with comma

Posted: 18 Oct 2010 06:48
by amel27
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

Re: error in tab/space with comma

Posted: 18 Oct 2010 07:48
by ghostmachine4
@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??

Re: error in tab/space with comma

Posted: 18 Oct 2010 11:49
by aGerman