Computing.Net > Forums > Programming > Batch file to isolate text blocks

Batch file to isolate text blocks

Reply to Message Icon

Original Message
Name: feedmegarbage
Date: September 25, 2007 at 15:23:10 Pacific
Subject: Batch file to isolate text blocks
OS: Windows XP Pro
CPU/Ram: Pentium 4 / 2GB
Model/Manufacturer: Dell
Comment:

Hey all, i'm trying to write a batch file that will read a text document (we'll say input.txt) and can isolate a block of the text within it, and port just that information to a new text document (we'll call it input2.txt.

For example, input.txt contains:
Hello
My name is what
What did you say
I dont even know
anymore

I just need to get the lines that are between 'Hello' and 'Anymore'. The problem i'm running into is that the number of lines between 'Hello' and 'Anymore' will vary from file to file that will be processed, eg, input.txt may say:
Hello
my name
is
aaron
and
i can't
program
anymore

Any guru out there wanna take a stab at this one? The only help i could find was to use sed.exe, and i've tried, but it doesn't work either. Unfortunately, i can't use VB or Perl to do this either, as this will need to be processed within another, already created and functional batch file.


Curious...


Report Offensive Message For Removal

Response Number 1
Name: Razor2.3
Date: September 25, 2007 at 17:42:19 Pacific
Subject: Batch file to isolate text blocks
Reply: (edit)

Well, you know, you COULD call a VBScript, and the batch file will wait for it.

That said, one .CMD, 13 lines:

@ECHO OFF
setLocal
for /f "delims=:" %%a in ('findstr /n "Hello" input.txt') do SET /A start=%%a - 1
SET start=skip=%start%
IF "%start%"=="skip=0" SET start=
for /f "delims=:" %%a in ('findstr /n "anymore" input.txt') do set end=%%a
(for /f "%start% delims=" %%a in (input.txt) do call :doWork %%a) > out.txt
exit /b

:doWork
IF %end% LEQ 1 exit /b
SET /A end -= 1
echo %*


Report Offensive Follow Up For Removal

Response Number 2
Name: ghostdog
Date: September 25, 2007 at 20:00:25 Pacific
Subject: Batch file to isolate text blocks
Reply: (edit)

well, if you can use sed.exe, i am sure you can download gawk.exe and use it too...it might be simple as this:
[code]
c:\> gawk "/Hello/,/anymore/" yourinputfile > outfile
[/code]


Report Offensive Follow Up For Removal

Response Number 3
Name: robcooper2001
Date: October 29, 2007 at 08:38:31 Pacific
Subject: Batch file to isolate text blocks
Reply: (edit)

I have a similar issue but I wish to replace the text to start :-
(SETVAR "NOMUTT" 1)
and the text to finish :-
(SETVAR "NOMUTT" 0)
and the text in between :-
(load "G:\\CAD\\ARCHITECTURAL\\2006 Standard Details\\CONTROL\\ARCHLOAD.lsp" -T )
These lines of text have caused problems because of the spaces and the quotes.
Also, I must make it search through a hard drive, find one of the files it may or may not be contained in - Acaddoc.lsp or acad2006doc.lsp etc,
BUT only if it is present.
IF NOT present, place the text ( or whatever new text/file path we choose after the refit ) into the located file.
This batch file must then copy itself to the startup to run everytime a user logs on!!
Thanks in advance...


robcooper2001


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 file to isolate text blocks

Comments:

 


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