Computing.Net > Forums > Programming > Remove text file lines

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.

Remove text file lines

Reply to Message Icon

Name: alastor27
Date: May 6, 2006 at 08:47:09 Pacific
OS: windows xp
CPU/Ram: 768
Comment:

Hello,
i need to cancel the first 4 empty lines of 6000 text files with the same structure.Someone can help me?Maybe with a script that is able to do so and save te result?

Is it possible to do it with a batch file?(i qould not be able to do it...)



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: May 7, 2006 at 03:39:56 Pacific
Reply:

I think it can be done in batch.

By "the first 4 empty lines" do you mean blank lines like this:

####
one
two

three
four
five
#######


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

M2Go



0

Response Number 2
Name: alastor27
Date: May 7, 2006 at 07:06:21 Pacific
Reply:

Hi,
thanks for reply.I mean blank lines...without any character...


0

Response Number 3
Name: dtech10
Date: May 7, 2006 at 08:10:26 Pacific
Reply:

Hi
When you say cancel I'm asumming you mean delete the blank lines.
Are the 4 blank lines at the begining of each file or scattered throughtout the files.


0

Response Number 4
Name: Mechanix2Go
Date: May 7, 2006 at 09:30:01 Pacific
Reply:

How big are the files?


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

M2Go



0

Response Number 5
Name: alastor27
Date: May 8, 2006 at 06:12:29 Pacific
Reply:

Yes,i mean delete.The blank lines are at the beginning of each text file and each text file is less than 10KB.


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: May 8, 2006 at 07:26:00 Pacific
Reply:

Hi,

This simple script will delete all the blanl lines, no matter where they are in the file. Is that what you need?

:: noblank.bat
@echo off

for %%F in (*.txt) do call :sub1 %%F
goto :eof

:sub1

set outfile=%~n1.out
type nul > %outfile%
for /f "tokens=*" %%L in (%1) do (
echo %%L>> %outfile%
)
goto :eof
:: DONE



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

M2


0

Response Number 7
Name: alastor27
Date: May 8, 2006 at 07:45:49 Pacific
Reply:

Thanks a lot Mechanix2Go but i need something that deletes only the first 4 blank lines of each text file...


0

Response Number 8
Name: Mechanix2Go
Date: May 8, 2006 at 09:02:42 Pacific
Reply:

Hi,

We're still not quite clear. Can you send me a couple of the files?


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

M2


0

Response Number 9
Name: dtech10
Date: May 8, 2006 at 09:51:54 Pacific
Reply:

Hi Carl
If the first 4 lines are blank and these are to
be deleted but you want to keep any blank lines
after this, try this on some dummy text files
for safety.


@echo off
for %%a in (*.txt) do (
type %%a | find /n /v "~" > ~Temp1.dat
type nul > ~Temp2.dat
for /f "skip=4 tokens=1,* delims=[]" %%c in (~Temp1.dat) do (
if "%%d"=="" ( echo. >> ~Temp2.dat
) else ( echo %%d >> ~Temp2.dat)
)
del %%a
type ~Temp2.dat > %%a
)
del ~Temp?.dat


0

Response Number 10
Name: alastor27
Date: May 8, 2006 at 13:44:00 Pacific
Reply:

THANKS dtech10!!

Your batch file is working perfectly!!!Thanks a lot!


0

Response Number 11
Name: dtech10
Date: May 9, 2006 at 05:36:10 Pacific
Reply:

Hi alastor27
Something I forgot to mension that the line

type %%a | find /n /v "~" > ~Temp1.dat

If there's a "~" in your files that line will be deleted.
If there is, change the "~" to another charactor that you know will not be in any of your files.


0

Response Number 12
Name: dtech10
Date: May 9, 2006 at 05:37:06 Pacific
Reply:

Hi alastor27
Glad it worked ok.
Something I forgot to mension that the line
type %%a | find /n /v "~" > ~Temp1.dat
If there's a "~" in your files that line will be deleted.
If there is, change the "~" to another charactor that you know will not be in any of your files.


0

Response Number 13
Name: Mechanix2Go
Date: May 9, 2006 at 09:32:30 Pacific
Reply:

Hi dtech10,

Im having no luck with your bat.

Original:
#####################
EUR Euro

GBP United Kingdom Pounds

CAD Canada Dollars

AUD Australia Dollars

JPY Japan Yen

INR India Rupees

NZD New Zealand Dollars

CHF Switzerland Francs

ZAR South Africa Rand
AFN Afghanistan Afghanis
ALL Albania Leke
DZD Algeria Dinars
ARS Argentina Pesos
AUD Australia Dollars
BSD Bahamas Dollars
######################

result:

######################
CAD Canada Dollars

AUD Australia Dollars

JPY Japan Yen

INR India Rupees

NZD New Zealand Dollars

CHF Switzerland Francs

ZAR South Africa Rand
AFN Afghanistan Afghanis
ALL Albania Leke
DZD Algeria Dinars
ARS Argentina Pesos
AUD Australia Dollars
BSD Bahamas Dollars
BHD Bahrain Dinars
BDT Bangladesh Taka
BBD Barbados Dollars
BMD Bermuda Dollars
BRL Brazil Reais
############

They're bigger, but that's the interesting part.


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

M2


0

Response Number 14
Name: dtech10
Date: May 9, 2006 at 13:01:40 Pacific
Reply:

Hi Mechanix
Computers who'ed buy such a tempermental devices.
Are you recieving error message's are you not sure how it works.



0

Response Number 15
Name: Mechanix2Go
Date: May 9, 2006 at 13:28:10 Pacific
Reply:

Hi dtech10,

No errors. Here's the before & after on a smaller file:

### before
EUR Euro

GBP United Kingdom Pounds

CAD Canada Dollars

AUD Australia Dollars

JPY Japan Yen

INR India Rupees

NZD New Zealand Dollars

CHF Switzerland Francs

ZAR South Africa Rand
AFN Afghanistan Afghanis
ALL Albania Leke
### end before

### after
CAD Canada Dollars

AUD Australia Dollars

JPY Japan Yen

INR India Rupees

NZD New Zealand Dollars

CHF Switzerland Francs

ZAR South Africa Rand
AFN Afghanistan Afghanis
ALL Albania Leke
### end after

Looks like it removes the first 4 lines regardless of whether they are blank or non-blank.


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

M2


0

Response Number 16
Name: dtech10
Date: May 9, 2006 at 14:57:56 Pacific
Reply:

Hi Mechanix
This would do the trick.

@echo off
setlocal EnableDelayedExpansion
set Count=0
for %%a in (*.txt) do (
type %%a | find /n /v "~" > ~Temp1.dat
type nul > ~Temp2.dat
for /f "tokens=1,* delims=[]" %%c in (~Temp1.dat) do (
if "%%d"=="" (
set /a Count=!Count!+1
if !Count! GTR 4 echo. >> ~Temp2.dat

) else ( echo %%d >> ~Temp2.dat )
)
del %%a
type ~Temp2.dat > %%a
)
del ~Temp?.dat
setlocal


0

Response Number 17
Name: Mechanix2Go
Date: May 9, 2006 at 16:30:10 Pacific
Reply:

Hi dtech10,

Yup, except the last line needs to be endlocal.

Thanks.


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

M2


0

Response Number 18
Name: kkjensen1
Date: June 29, 2006 at 13:28:40 Pacific
Reply:

I am trying to remove all the blank lines in a file but am having trouble with the first set since it's replacing all my blank lines with "Echo is off."

So I guess the blank lines are gone but this isn't what I had in mind.


0

Sponsored Link
Ads by Google
Reply to Message Icon






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: Remove text file lines

vbscript to parse text file and com www.computing.net/answers/programming/vbscript-to-parse-text-file-and-com/16691.html

Extracting lines for a text file www.computing.net/answers/programming/extracting-lines-for-a-text-file/11315.html

Remove top 6 lines of a text file www.computing.net/answers/programming/remove-top-6-lines-of-a-text-file/19324.html