Computing.Net > Forums > Programming > delete x number of lines from a CSV file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

delete x number of lines from a CSV file

Reply to Message Icon

Name: ChalkyDave
Date: August 13, 2009 at 07:07:55 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

Hi,
Is it possible to delete x number of lines from a CSV file using a batch file ?

What I have is 2 CSV files with a Log of temperatures from an AirCon unit.
The first is an older file contain several months worth of data, the second contains all the original data from the first, but with the next months data tagged onto the bottom of the file.

I am hoping to count the number of lines in the first file using something like:

for /f "delims=" %%i in ('type AC1-Count.txt^|find /c /v "" ') do set count=%%i
echo %count%

but then I want to delete this number of lines from the second file starting at line number 2.

I hope I have explained that in some sort of english.

Thanks in advance

Dave




Sponsored Link
Ads by Google

Response Number 1
Name: dtech10
Date: August 13, 2009 at 10:42:58 Pacific
Reply:

Hi Chalkydave
Is this what you want

@echo off
SetLocal EnableDelayedExpansion

for /f "delims=" %%i in ('type AC1-Count.txt ^|find /c /v ""') do set Count=%%i
echo %Count%

set Count2=0
for /f "tokens=*" %%a in (AC1-Count2.txt.) do (
if !Count2! EQU 0 (echo %%a)
if !Count2! GTR %Count% (echo %%a)
set /a Count2+=1
) >> tmp.txt

rem rename out after testing if required
rem del AC1-Count.txt
rem ren tmp.txt AC1-Count.txt


0

Response Number 2
Name: ChalkyDave
Date: August 14, 2009 at 00:55:04 Pacific
Reply:

dtech10,
You are the man !

I had to tinker with it slightly, but this is the working code:

@echo off
SetLocal EnableDelayedExpansion

for /f "delims=" %%i in ('type 2009_07_01.csv ^|find /c /v ""') do set Count=%%i
echo %Count%
set /a Lines=%Count%-1
echo %Lines%
set Count2=0
for /f "tokens=*" %%a in ('type 2009_08_01.csv') do (
if !Count2! EQU 0 (echo %%a)
if !Count2! GTR %Lines% (echo %%a)
set /a Count2+=1
) >> 2009_08.csv

The only thing I need to try and work out now, is how to set the dates automatically.

i.e. On the first of each month, this batch file uses the current months data against the previous months.

Or, to do the same using the latest two files regardless of the names. (Extension would always be .CSV)

Cheers

Dave


0

Response Number 3
Name: ChalkyDave
Date: August 17, 2009 at 07:54:36 Pacific
Reply:

Hi all,

Does anybody know why the code below will work for a time and then stop working ?

@echo off
SetLocal EnableDelayedExpansion

Rem Get Day,Mth & Year from %Date%
set Day=%Date:~0,2%
set Mth=%Date:~3,2%
set Yr=%Date:~6,4%
if %Mth% LEQ 9 set Mth=%Date:~4,1%
if %Mth% LEQ 9 (
set Mth2=0%Mth%
) else (
set Mth2=%Mth%
)
set /a xMth=%Mth%-1
if %xMth% LEQ 9 (
set xMth2=0%xMth%
) else (
set xMth2=%xMth%
)
if %Mth% LEQ 1 (
set xMth2=12
set /a xYr2=%Yr%-1
) else (
set xYr2=%Yr%
)
echo This Months File = AC1-%Yr%_%Mth2%_01
echo Last Months File = AC1-%xYr2%_%xMth2%_01

for /f "delims=" %%i in ('type AC1-%Yr%_%Mth2%_01.csv ^|find /c /v ""') do set Count=%%i
echo %Count%
set /a Lines=%Count%-1
echo %Lines%
set Count2=0
for /f "tokens=*" %%a in ('type AC1-%xYr2%_%xMth2%_01.csv') do (
if !Count2! EQU 0 (echo %%a)
if !Count2! GTR %Lines% (echo %%a)
set /a Count2+=1
) >> AC1-%Yr%_%Mth2%.csv


0

Response Number 4
Name: dtech10
Date: August 17, 2009 at 15:12:36 Pacific
Reply:

Hi ChalkyDave

It's the old leading zero used with set /a command which it takes as being an Octal number.
Enter the extra lines below and see if that cures your problem.
If you need the leading zeros for display. Use this
-------------------------------------------------
If %Day% LSS 10 (set Day=0%Day%)
If %Mth% LSS 10 (set Mth=0%Mth%)
-----------------------------------------------

Your Code
-------------
Rem Get Day,Mth & Year from %Date%
set Day=%Date:~0,2%
if "!Day%:~0,1!" EQU "0" (set Day=!Day:~1,1!)
set Mth=%Date:~3,2%
if "!Mth%:~0,1!" EQU "0" (set Mth=!Mth:~1,1!)
set Yr=%Date:~6,4%


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: delete x number of lines from a CSV file

Batch to del first x lines of text www.computing.net/answers/programming/batch-to-del-first-x-lines-of-text/16887.html

Delete blank lines from a text file www.computing.net/answers/programming/delete-blank-lines-from-a-text-file/14525.html

Batch - Getting the number of lines www.computing.net/answers/programming/batch-getting-the-number-of-lines/15728.html