Hello, how to delete 2nd line from a csv using a bat?

October 15, 2015 at 10:47:33
Specs: Windows 7

I have a CSV that I need to delete a line just below the Header (the second row of the csv) using a bat file. However, I have no idea how to do this.

Does anyone have an idea how this can be done?

Thank you for your help

See More: Hello, how to delete 2nd line from a csv using a bat?

October 16, 2015 at 03:13:54
Not tested.

Note that FOR skips blank lines

setlocal enabledelayedexpansion
set count=1
for /f %%A in (file.csv) do (
if not !count!==2 echo %%A>>newfile.csv
set /a count=!count!+1
:: Replace original with new if required. Remove "REM"
REM del /q file.csv
REM ren newfile.csv file.csv

Pro tip: Marking answers that solve, or help helps you solve a problem as "Best Answer" also helps other people solve similiar ones in the future.

message edited by RainBawZ

Report •

October 16, 2015 at 03:36:42

@echo off & setlocal
set /P header=< "mycsv.csv"
(echo.%header%) > "newcsv.csv"
for /F "skip=2 delims=" %%i in ('type "mycsv.csv"') do (echo.%%i) >> "newcsv.csv"
del "mycsv.csv"
ren "newcsv.csv" "mycsv.csv"

Report •

October 16, 2015 at 04:50:17

Thank you very much for the two scripts. My apologies, I am a noob at this. I tried them both without success. My csv looks look this:

# Transaction,Application,Tier type,Unique users,Operation time (ms),Application performance (%),Affected users (performance),Percentage of affected users (performance) (%),Failures (total),Availability (total) (%),Percentage of affected users (availability) (%),Affected users (availability),Attempts,Operations,Slow operations,
# Column Ids: bgTrans,bgAppl,bgnameTierType,CliCnt,transTime,appPerf,AffUserAppl,AffUserApplPerc,FA,Avb,AffUserAvlPerc,AffUserAvl,Att,trans,slowTrans,

All other,CompanyName,Data center

I need to find a way to delete where it says # Column Ids: all the way to slowTrans. as I am not able to import it into the program I'm using as it sees two lines of Headers.

Thanks again for the time taken to help me with this. If additional details are needed, please just let me know.

Thank you,

Report •

Related Solutions

October 16, 2015 at 19:30:58
rem set col.heads=1,2,3,4,5,etc or whatever you want, or nothing
MORE +3 file1.csv>>file2.csv

might work, IF your csv file is not unicoded, and IF your line-breaks are recognized by MORE. If your app REALLY has to have column heads, you can just echo them to the file before the MORE statement. You should take a look at your csv file to see if it's Unicode, and what line-breaks it uses. I recommend EDIT /70 to examine the file because it shows Unicode interstices as well as line-break char.s
Another way is to use either FIND or FINDSTR with line numbers and then exclude those line numbers from your output. f/e: FIND /N /V "" file.csv|FIND /V "[3]"
would exclude line 3 from output. If "[3]" occurs in the base-file stream, then use : FIND /N /V "" file.csv|FINDSTR "^\[3\]"
which will limit [3] to the beginning of the line and not mid-stream.

message edited by nbrane

Report •

Ask Question