batch to delete text file header

February 21, 2007 at 08:30:18
Specs: winXP, 3.2GHz 1GB
Hi
Does anyone know how i can delete 5 lines of header from a text file from all the text files in a folder.
Thanks


See More: batch to delete text file header

Report •


#1
February 21, 2007 at 08:56:08
@echo off
setLocal EnableDelayedExpansion

if exist *.new del *.new

for /f "tokens=* delims= " %%A in ('dir /b *.txt') do (
set NAME=%%~nA
call :sub1 %%A
)
goto :eof

:sub1

for /f "tokens=* skip=5 delims= " %%L in (%*) do (
echo %%L >> !NAME!.new
)
goto :eof



=====================================
If at first you don't succeed, you're about average.

M2



Report •

#2
February 21, 2007 at 09:20:31
This is a bit neater:

::== s7.bat
@echo off
setLocal EnableDelayedExpansion

if exist *.new del *.new

for /f "tokens=* delims= " %%A in ('dir /b *.txt') do (
set NAME=%%~nA
set fileNAME=%%A
for /f "tokens=* skip=5 delims= " %%L in (!fileNAME!) do (
echo %%L >> !NAME!.new
)
)
::==


=====================================
If at first you don't succeed, you're about average.

M2



Report •

#3
February 22, 2007 at 02:48:26
hi thanks a lot for the very speedy reply. i didnt really explain myself too well. i have a lot of text files in a folder and every one has a header, of not 5 lines but actually 5 tabs. i want a batch file that can delete this first section in each file. also can you tell me a good website that will explain the script to me a bit becasue i am completely unfamiliar with batch language
thanks

Report •

Related Solutions

#4
February 22, 2007 at 02:53:55
Not clear on '5 tabs'.



=====================================
If at first you don't succeed, you're about average.

M2



Report •

#5
February 22, 2007 at 03:12:14
ok sorry i dont think they are tabs, im not too clued up on ascii but they display in notepad as a rectangle that i thought was a tab because it doesnt return the carriage. anyway below i pasted an example of a few characters from the first line: basically i want to delete everything before the first four digit integer


Filename : C:\Microdensitometer\meetresultaten\Batch2\Measurement_15_02_1 .txt
Mean : 2158.879759
Standard deviation : 201.380930
Variance : 40554.278965
Data:
2228 2282 2308 2304 2268 2244 2218 2208 2206 2208 2212 2218 2232 2254 2250 2220 2184 2170 2174 2210 2228 2214 2162 2140 212


Report •

#6
February 22, 2007 at 03:19:02
Can you zip up and email the file[s]. But rename to my.z because I have zips blocked.


=====================================
If at first you don't succeed, you're about average.

M2



Report •

#7
February 22, 2007 at 04:34:35
Not all the txt files you sent have headers, as defined by blocks like this:

Filename : C:\Microdensitometer\meetresultaten\Batch2\Measurement_15_0
Mean : 1654.450985
Standard deviation : 288.774750
Variance : 83390.856166
Data:

But regardless, if the file formats are consistent in that the 'header' lines all contain colons [and no others do] this should work:

::== stripHED.bat
@echo off
setLocal EnableDelayedExpansion

if exist *.new del *.new

for /f "tokens=* delims= " %%A in ('dir /b *.txt') do (
set pre=%%~nA
set fname=%%A
for /f "tokens=* delims= " %%N in ("!fname!") do (
type "%%N" | find /v ":" >> !pre!.new
)
)
::==


=====================================
If at first you don't succeed, you're about average.

M2



Report •

#8
February 22, 2007 at 05:29:35
Well... not so fast. That script is truncating the files. My guess is that FIND doesn't have enough horsepower.

Yes, FINDSTR says:

FINDSTR: Line 1 is too long

FIND isn't smart enough.


=====================================
If at first you don't succeed, you're about average.

M2



Report •

#9
February 22, 2007 at 21:26:12
If you want to try something other than a batch file, thiese perl commands will process each .txt file in the directory.

=============================================================

This extracts only the lines matching the groups of space separated digits.
perl -i.bak -e "BEGIN{@ARGV=<*.txt>}while(<>){print if /^(\d+ \d+)+$/}"

This one removes the first 5 lines in each file.
perl -i.bak -e "BEGIN{@ARGV=<*.txt>}while(<>){print if $. > 5}"


Report •

#10
February 23, 2007 at 02:21:05
Hi FM,

I was hoping you'd jump in here.

Apparently the first FOUR lines are the 'headers' but the line breaks are 0a [no 0d] whicj may be what tripped up FIND.


=====================================
If at first you don't succeed, you're about average.

M2



Report •

#11
February 23, 2007 at 06:08:02
Hi FM,

This one has really flattened me. Yes, there are FIVE lines of header. Sorry.


=====================================
If at first you don't succeed, you're about average.

M2



Report •

#12
February 23, 2007 at 08:27:10
Hi M2,

>> I was hoping you'd jump in here.

I've been monitoring some of the batch file questions but refrained from posting because most people only want a batch solution. In this case it looked like the batch solution was going to be troublesome.

********

I goofed on the second example...it only works on the first file it edits. Since it does an implicit close on each file instead of an explicit close, the line number variable $. doesn't get reset back to 0 when it opens the next file. If lawmate wants, I could fix it or work up another approach.


Report •

#13
February 23, 2007 at 09:15:56
Hi guys,

I did some banging around and found Bruce Guthrie's BFIND here:

http://www.sac.sk/files.php?d=16&p=9

This seems to do the deed without choking up like FIND:

bfind /v /-HEADER ":" d.txt > d.new


=====================================
If at first you don't succeed, you're about average.

M2



Report •

#14
February 23, 2007 at 09:35:35
Well... not so fast.

[Again; must be the phrase of the day.]

It won't reconize LFNs. Another reason to use 8.3 as if another was needed.


=====================================
If at first you don't succeed, you're about average.

M2



Report •

#15
May 12, 2007 at 02:00:52
does any one know how to delete some of the selected lines from a text file...using dos command

Report •

#16
May 12, 2007 at 02:13:28
What version of DOS and how will the lines be selected?



=====================================
If at first you don't succeed, you're about average.

M2



Report •


Ask Question