Batch file to delete lines from a file

Microsoft Windows xp professional sp2c e...
June 16, 2010 at 20:40:07
Specs: Windows 2003 or 2008
Hi Experts,

I have a file format as mentioned below. I
would like to have Batch script (Win 2K*) which
can:

1. Remove first 6 and last 3 lines of the file placed in D:/Test directory.
2. Delete the lines where 3rd column which
has value as Alpha Numeric
3. Delete the lines where 4th column having
0.00
4. Should delete all cotes (")

Sample File Snippet

"RPT TRIALB ABC LIMITED"
"TRIAL BALANCE"
"FOR THE PERIOD ENDED DECEMBER 2009"
""
""
""
"1","Bank1","1234",100.00,XYZ
"2","Bank2","ABC1234",200.00,XYZ
"3","Bank3","1234",0.00,XYZ
"4","Bank4","CDE1234",400.00,XYZ
"5","Bank5","1234",500.00,XYZ
"","","",,,,
"","","",,,,
"END OF REPORT"

Output should be like this.

1,Bank1,1234,100.00,XYZ
5,Bank5,1234,500.00,XYZ

Thanks,
Phani Akella

Thanks,
Phani Akella


See More: Batch file to delete lines from a file

Report •

#1
June 17, 2010 at 09:43:48
@echo off & setlocal enabledelayedexpansion
for /f "skip=6 tokens=1-5 delims=," %%a in (D:\test\file.txt) do (
if %%a equ "" goto :eof
if "%%d" neq "0.00" (
for /f "tokens=* delims=0123456789" %%z in (%%c) do set test=%%z
if "!test!" equ "" >> new echo %%~a,%%~b,%%~c,%%d,%%e
)
)
::----- end
not tested yet.

Report •

#2
June 17, 2010 at 16:11:41
Hi,

I tried executing the code. It is not working. Can you please kindly look into this. Thank you.

Thanks,
Phani Akella


Report •

#3
June 17, 2010 at 18:27:50
It worked over here, with the test data you gave.
Can you pls provide some more specific error/output information? And the contents of "new" (based on your sample data, as well as your real data if it differs much).
help me out here!

Report •

Related Solutions

#4
July 7, 2010 at 15:42:34
Hi nbrane,

Thank you very much for your help.The script is working fine for the structure which I have pasted before. But there is slight change in the structure. It is not working for the file which I am pasting below: Please kindly help.

"ABC TRIALB XYZ ENERGY LIMITED"
" TRIAL BALANCE"
" FOR THE PERIOD ENDED MAY 2010"
""
""
""
"1","A!!!!!!BC$$$ Bank","101000",10000.00,0.00,50000.00,0.00
"2","PQR Bank","102000",20000.00,0.00,60000.00,0.00
"3","XYZ Bank","CC000",30000.00,0.00,70000.00,0.00
"4","ABC Bank","ABC000",40000.00,0.00,80000.00,0.00
"5","PQR Bank","101200",0.00,0.00,50000.00,0.00
"6","XYZ Bank","101250",0.00,0.00,50000.00,0.00
"","","",,,,
"","","",,,,
"END OF REPORT"

My requirement is:

Script should go to the folder path (D:/BankTransactions/BankDetails_*.*) and check for the file with name BankDetails_*.* and with out changing the name it has to do the below mentioned activities.

1) First 6 and Last 3 Lines should be deleted.

2) Rows with Alpanumeric values in 3rd column (Account Number) should be deleted.

eg:
Rows 3,4 should be deleted, because 3rd coloum has a alphanumeric value.

3) Rows with 0.00 in 4th column (Amount) should be deleted.

eg:
Rows 5,6 should be deleted because coloumn 4th (Amount) value is 0.

4) Remove Special charecters like ! and $.

So, My output should look like below with out changing the name of the file.

"1","ABC Bank","101000",10000.00,0.00,50000.00,0.00
"2","PQR Bank","102000",20000.00,0.00,60000.00,0.00


Thanks,
Phani Akella


Report •

Ask Question