Delete all but first line in text files

April 28, 2011 at 07:54:53
Specs: Windows Vista
Hello! I'm trying to extract e-mail addresses from a whole set of bounceback reports so I can send a list of bad addresses to a client (not spamming people, by the way... we do e-mail blasts to members of their loyalty programs and they want to keep their records updated).

The failed address is always on line 38 of the report. I found a batch here that, with a quick modification, allowed me to delete everything before the e-mail address:

http://www.computing.net/answers/pr...

Now I have text files whose first lines are e-mail addresses. I now need a batch file that will delete everything except the first line. So far what I've found on the forum here are people trying to delete the first line and leave everything else; I need exactly the opposite. Thanks in advance to anyone who can help!


See More: Delete all but first line in text files

Report •

#1
April 29, 2011 at 00:14:30
Save yourself all that lost motion. Get line 38 from the original file.

:: ==========================================
::
:: alexg.bat Fri 29-04-2011 14:09:54.56
@echo off > newfile & setLocal enableDELAYedeXpansioN

for /f "skip=37 tokens=* delims= " %%a in (datafile) do (
> newfile echo.%%a
goto :eof
)


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
April 29, 2011 at 07:29:20
Did I need to modify any variables in that script? Obviously I can change 37 to a different number if I need to keep a different line, but was there anything else? Running the .bat just caused the command prompt to flash open for a second, and then produced an empty file called newfile (no extension) with nothing in it. But, I'm relatively new to this, so the simplest answer is that I probably did something wrong inadvertantly.

Just in case, I'm including one of the files to see if there's anything in there - special character, etc - that would cause issues. Thanks for your reply though... a simpler answer is always preferable! In the example, jenstock@earthlink.net is the line we're pulling.

X-MDAV-Processed: mail.dmx-direct.com, Wed, 13 Apr 2011 14:47:36 -0600
Return-path: <>
Received: from web1.dmx-direct.com ([65.38.187.116])
by mail.dmx-direct.com (mail.dmx-direct.com)
(MDaemon PRO v11.0.3)
with ESMTP id md50000070015.msg
for <mariap@dmx-direct.com>; Wed, 13 Apr 2011 14:47:27 -0600
Authentication-Results: mail.dmx-direct.com
x-ip-mail=pass smtp.mail= (ip=65.38.187.116)
X-Spam-Processed: mail.dmx-direct.com, Wed, 13 Apr 2011 14:47:27 -0600
(not processed: spam filter heuristic analysis disabled)
X-MDMailLookup-Result: pass smtp.mail= (ip=65.38.187.116) (mail.dmx-direct.com)
X-Rcpt-To: mariap@dmx-direct.com
X-MDRcpt-To: mariap@dmx-direct.com
X-MDRemoteIP: 65.38.187.116
X-Return-Path:
X-Envelope-From:
X-MDaemon-Deliver-To: mariap@dmx-direct.com
From: postmaster@web1.dmx-direct.com
To: mariap@dmx-direct.com
Date: Wed, 13 Apr 2011 14:40:55 -0600
MIME-Version: 1.0
Content-Type: multipart/report; report-type=delivery-status;
boundary="9B095B5ADSN=_01CBDA21CC39E1F40000022Dweb1.dmx?direct."
X-DSNContext: 7ce717b1 - 1196 - 00000002 - 00000000
Message-ID: <goZolmRBa000001c9@web1.dmx-direct.com>
Subject: Delivery Status Notification (Failure)

This is a MIME-formatted message.
Portions of this message may be unreadable without a MIME-capable mail program.

--9B095B5ADSN=_01CBDA21CC39E1F40000022Dweb1.dmx?direct.
Content-Type: text/plain; charset=unicode-1-1-utf-7

This is an automatically generated Delivery Status Notification.

Delivery to the following recipients failed.

jenstock@earthlink.net


--9B095B5ADSN=_01CBDA21CC39E1F40000022Dweb1.dmx?direct.
Content-Type: message/delivery-status

Reporting-MTA: dns;web1.dmx-direct.com
Received-From-MTA: dns;web1.dmx-direct.com
Arrival-Date: Wed, 13 Apr 2011 14:40:55 -0600

Final-Recipient: rfc822;jenstock@earthlink.net
Action: failed
Status: 5.5.0


Report •

#3
April 30, 2011 at 02:44:40
There's probably an error. But until you run it at the prompt, you won't know.


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
May 2, 2011 at 12:45:06
OK, I ran the .bat from the command prompt and got the error:

"The system cannot find the file datafile."


Report •

#5
May 4, 2011 at 14:45:45
OK, I figured out one of the issues. I changed the batch so that instead of datafile, the "in () do" command pointed right to one of the files:

:: ==========================================
::
:: alexg.bat Fri 29-04-2011 14:09:54.56
@echo off > newfile & setLocal enableDELAYedeXpansioN

for /f "skip=37 tokens=* delims= " %%a in (md50000001135.txt) do (
> newfile echo.%%a
goto :eof
)

That worked fine. Grabbed the line I wanted, no problem, and put it in the file called newfile. Now, how can I get it to do the same process for every file in the directory?


Report •

#6
May 6, 2011 at 10:25:30
Do you want the output lines all in ONE file?


=====================================
Life is too important to be taken seriously.

M2


Report •

#7
May 6, 2011 at 10:37:15
Yes, with each pulled line as a new line in the output file. So:

abc@gmail.com
123@yahoo.com
zyx@aol.com


Report •

#8
May 6, 2011 at 11:01:11
:: ==========================================
::
:: alexg2.bat Fri 29-04-2011 14:09:54.56
@echo off > newfile & setLocal enableDELAYedeXpansioN

for /f "tokens=* delims= " %%a in ('dir/b') do (
call :sub1 %%a
)
goto :eof

:sub1
for /f "skip=37 tokens=* delims= " %%a in (%1) do (
>> newfile echo.%%a
goto :eof
)
goto :eof


=====================================
Life is too important to be taken seriously.

M2


Report •

#9
May 6, 2011 at 11:55:39
Thank you SO MUCH! That did exactly what I was looking for. I hope I didn't seem ungrateful for your work... especially considering you're providing your expertise free of charge. Thanks again for this... it'll save me so much time now.

Report •

Ask Question