0

I have below records in the source csv file and need to split into two files based on Column A using windows cmd.

Input:

4000,XXX,X11
1000,YYY,Y11
3000,ZZZ,Z11
4300,XXX,X12

Output:

File 1: (File 1 should be based on the column A where the values are starting with 4. Ex - 4000 and 4300)

4000,XXX,X11
4300,XXX,X12

File 2: Other records.

1000,YYY,Y11
3000,ZZZ,Z11
aschipfl
  • 33,626
  • 12
  • 54
  • 99
AravindhK
  • 9
  • 4
  • I tried with below script. But it not splitting based on the colume. setlocal EnableDelayedExpansion for /F "delims=," %%a in (Source.csv) do ( set line=%%a set line=!line:,,=, ,! set line=!line:,,=, ,! for /F "tokens=1 delims=," %%i in (^"!line!^") do ( echo %%i>>Final.csv ) ) – AravindhK Nov 27 '15 at 05:29

1 Answers1

0

get all lines starting with "4":

findstr /b "4" oldfile.csv>four.csv

get all lines not starting with "4":

findstr /v /b "4" oldfile.csv>rest.csv

edit to work with second column:

findstr /R /B ".*,4" oldfile.csv >four.csv
findstr /R /B /V ".*,4" oldfile.csv >rest.csv

Note: as aschipfl mentioned, it might be better to build a for loop, but that's more Code.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • How can i split the file If the data is comming in other columns? EX: XXX, 4000, X11 – AravindhK Nov 26 '15 at 12:36
  • 1
    please give a real example to get a working solution. Which other column? the second? Is there a space after the comma? (reliable?) – Stephan Nov 26 '15 at 12:45
  • @AravindhK, take a look at `for /F` -- type `for /?` in command prompt and read the help text carefully... – aschipfl Nov 26 '15 at 12:57
  • 1
    @aschipfl also possible without `for`. `findstr /R` should do the job (depends on the data) – Stephan Nov 26 '15 at 13:14
  • I agree, @Stephan, `findstr` is also possible (I don't like it that much though because it has got several [bugs](http://stackoverflow.com/q/8844868/5047996)); but I think `for /F "delims=,"` is just perfect for CSV-files (as long as there are no quoted values which contain `,` themselves; but `findstr` cannot handle such correctly either); – aschipfl Nov 26 '15 at 14:02
  • @Stephen: I may get the source file as "XXX,4000,X11" (delimited by Comma and there are no spaces. How can I split the file based on second column? 4000 - Company code. The file may contains multiple company code and i need to split the file based on company codes . – AravindhK Nov 27 '15 at 05:26