Computing.Net > Forums > Programming > batch to delete text file header

batch to delete text file header

Reply to Message Icon

Original Message
Name: lawmate
Date: February 21, 2007 at 08:30:18 Pacific
Subject: batch to delete text file header
OS: winXP
CPU/Ram: 3.2GHz 1GB
Model/Manufacturer: Dell
Comment:

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


Report Offensive Message For Removal


Response Number 1
Name: Mechanix2Go
Date: February 21, 2007 at 08:56:08 Pacific
Subject: batch to delete text file header
Reply: (edit)

@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 Offensive Follow Up For Removal

Response Number 2
Name: Mechanix2Go
Date: February 21, 2007 at 09:20:31 Pacific
Subject: batch to delete text file header
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 3
Name: lawmate
Date: February 22, 2007 at 02:48:26 Pacific
Subject: batch to delete text file header
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 4
Name: Mechanix2Go
Date: February 22, 2007 at 02:53:55 Pacific
Subject: batch to delete text file header
Reply: (edit)

Not clear on '5 tabs'.



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

M2



Report Offensive Follow Up For Removal

Response Number 5
Name: lawmate
Date: February 22, 2007 at 03:12:14 Pacific
Subject: batch to delete text file header
Reply: (edit)

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 Offensive Follow Up For Removal


Response Number 6
Name: Mechanix2Go
Date: February 22, 2007 at 03:19:02 Pacific
Subject: batch to delete text file header
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 7
Name: Mechanix2Go
Date: February 22, 2007 at 04:34:35 Pacific
Subject: batch to delete text file header
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 8
Name: Mechanix2Go
Date: February 22, 2007 at 05:29:35 Pacific
Subject: batch to delete text file header
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 9
Name: FishMonger
Date: February 22, 2007 at 21:26:12 Pacific
Subject: batch to delete text file header
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 10
Name: Mechanix2Go
Date: February 23, 2007 at 02:21:05 Pacific
Subject: batch to delete text file header
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 11
Name: Mechanix2Go
Date: February 23, 2007 at 06:08:02 Pacific
Subject: batch to delete text file header
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 12
Name: FishMonger
Date: February 23, 2007 at 08:27:10 Pacific
Subject: batch to delete text file header
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 13
Name: Mechanix2Go
Date: February 23, 2007 at 09:15:56 Pacific
Subject: batch to delete text file header
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 14
Name: Mechanix2Go
Date: February 23, 2007 at 09:35:35 Pacific
Subject: batch to delete text file header
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 15
Name: rohit.p
Date: May 12, 2007 at 02:00:52 Pacific
Subject: batch to delete text file header
Reply: (edit)

does any one know how to delete some of the selected lines from a text file...using dos command


Report Offensive Follow Up For Removal

Response Number 16
Name: Mechanix2Go
Date: May 12, 2007 at 02:13:28 Pacific
Subject: batch to delete text file header
Reply: (edit)

What version of DOS and how will the lines be selected?



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

M2



Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: batch to delete text file header

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge