xzibit15
xzibit15

Reputation: 81

split csv file vertically in batch

I need to split a csv file using a bat.

The file is like

a;b;c;d;e;f

I need to put the columns a;b into file1.csv and c;d;e;f into file2.csv using bat language.

this what i done :

@echo off & setlocal enabledelayedexpansion
set c=0
for /f "tokens=*" %%a in (file.csv) do (
  REM this next is just to kill any lingering left-overs
  >f!c!.mol echo.
)
pause

Upvotes: 0

Views: 149

Answers (2)

lit
lit

Reputation: 16226

Here is a .bat file script that splits the .csv file.

powershell -NoLogo -NoProfile -Command ^
    "Get-Content -Path '.\sv.txt' |" ^
        "ForEach-Object {" ^
            "if ($_ -match '([^;];[^;]);(.*)') {" ^
                "$Matches[1] | Out-File -FilePath 'sv-file1.txt' -Encoding ascii -Append;" ^
                "$Matches[2] | Out-File -FilePath 'sv-file2.txt' -Encoding ascii -Append;" ^
            "}" ^
        "}"

Upvotes: 0

Stephan
Stephan

Reputation: 56155

get the first two columns (tokens=1,2) to file1.csv and the rest (*) to file2.csv:

for /f "tokens=1,2,* delims=;" %%a in (file.csv) do (
  >>file1.csv echo %%a;%%b
  >>file2.csv echo %%c
)

Upvotes: 1

Related Questions