Solved Bat To Strip Lines From A Csv File

January 22, 2015 at 14:49:14
Specs: Windows 7
Hi there,

I have a large csv file (8GB) called input.csv, I am in need to create a bat file to export every 4th lines of the csv file. The filtered file will be called output.csv.

How do I edit the below script to perform the task?

::====== script starts here ===============
:: split csv lines begin with O
:: Osplit.bat 2013-06-27 14:55:59.31
@echo off & setLocal enableDELAYedeXpansioN
@echo off > A.csv & @echo off > B.csv

:main
for /f "tokens=* delims= " %%a in (my.csv) do (
set S=%%a
set S=!S:~0,1!
if !S! equ O (
>> A.csv echo.%%a
) else (
>> B.csv echo.%%a
)
)
goto :eof
::====== script ends here =================


See More: Bat To Strip Lines From A Csv File

Report •

#1
January 23, 2015 at 05:44:21
Your request can be easily solved, just specify better what you mean by "to export every 4th lines of the csv file".

By rhe way there is no DOS in Windows 7 since it is a NT kernel system and your batch is actually a NT batch, more advanced and not supported by native MS DOS.


Report •

#2
January 23, 2015 at 08:00:20
my csv file has 4,000,000 rows of data. I would like to export every 4th row i.e 1, 4, 8, 12 and so on. The end result is an output file with only 1,000,000 rows of data.

Report •

#3
January 23, 2015 at 09:33:58
✔ Best Answer
caveat: tested on a small file.

::====== script starts here ===============
:: get every 4th line
:: fourth.bat 2015-01-24 0:23:43.69
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

set/a N=0
:main
for /f "tokens=* delims= " %%a in (myfile) do (
set/a N+=1 & set/a C=N%%4
if !C! equ 0 echo.%%a
) >> NEWFILE
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

Related Solutions

#4
January 23, 2015 at 12:32:11
It works great thank you.

Report •

Ask Question