Computing.Net > Forums > Disk Operating System > extract text block from a text file

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.

extract text block from a text file

Reply to Message Icon

Name: sushilraina
Date: October 30, 2007 at 13:46:39 Pacific
OS: winxp
CPU/Ram: celeron 512
Product: intel
Comment:

i have a text file with about 2000 lines. how can i sort the lines (based on first alphabet in each line) and then extract block of text starting from (A to M) and (N to Z)? that means there will be 2 output files, one containing all text lines starting from A upto M and the other containing all text lines starting from N upto Z.
thanks


sushilraina



Sponsored Link
Ads by Google

Response Number 1
Name: cup
Date: October 30, 2007 at 15:07:27 Pacific
Reply:

First part. Say your file is called mixed.txt

sort mixed.txt > sorted.txt

Second part

findstr "^[A-M]" sorted.txt > ATOM.txt
findstr "^[N-Z]" sortex.txt > NTOZ.txt


0

Response Number 2
Name: Mechanix2Go
Date: October 31, 2007 at 07:55:41 Pacific
Reply:

Hi cup,

good one



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

M2



0

Response Number 3
Name: sushilraina
Date: November 5, 2007 at 08:02:11 Pacific
Reply:

cup, thanks a lot for your answer. it helped me a lot to solve my problem and i learned a lot from it.

i renamed my batch file "cup.bat" after using your hint.

thanks,
sushilraina


0

Response Number 4
Name: s.arora
Date: May 4, 2008 at 19:33:23 Pacific
Reply:

Is there a way by which we can extract few lines from a text file based on a condition.

E.g A file has 2000 lines and we would like to search for word "internet". Once this word is found, we need 10 lines before and after this word.

Thanks

Supreet Arora


0

Response Number 5
Name: Mechanix2Go
Date: May 5, 2008 at 05:36:13 Pacific
Reply:

I'm amazed you were able to post a reply in a thread this old.

I recommend a new one. In case the magic wears off.

::==================================
If the keyword occurs only once::

::==

:: get 10 lines before and fter keyword

@echo off > newfile
setLocal EnableDelayedExpansion

for /f "tokens=1 delims=[]" %%a in ('find /n "internet" ^< biglog') do (
set /a K=%%a
)

set /a S=!K!-10
set /a E=!K!+10

for /f "tokens=* delims= " %%i in (biglog) do (
set /a N+=1
for /L %%L in (!S! 1 !E!) do (
if !N! equ %%L echo %%i >> newfile
)
)


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

M2


0

Related Posts

See More



Response Number 6
Name: G2
Date: August 6, 2008 at 13:06:22 Pacific
Reply:

How about multiple instance of word “start” and “end” in a file? But each set of start and end needs to be outputted to a single file. For instance start1 goes with end1, etc. start1 and end1 doesn’t have a consistent location in the file. Pretty much what I need is to separate multiple files (identified by the start and end) inside a single file.

G2


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 Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: extract text block from a text file

reading a file from a batch file www.computing.net/answers/dos/reading-a-file-from-a-batch-file/765.html

Display date from a batch file www.computing.net/answers/dos/display-date-from-a-batch-file/2143.html

Detect directory from a batch file.. www.computing.net/answers/dos/detect-directory-from-a-batch-file/4010.html