Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 02-27-2007, 05:35 PM   #1
Registered: Apr 2004
Posts: 340

Rep: Reputation: 30
extracting a chunk of text from a large text file

I have a very large text file. Over 47,000 lines.

I need approx 50 consecutive lines from it.

This chunk of lines
begins with the text "----BEGIN_SEGMENT----"
ends with the text "----END_SEGMENT----"

Can you help me with a bash script that will remove everthing
before the text "----BEGIN_SEGMENT----"
after the text "----END_SEGMENT----"

So that I can extract exactly what I need.

Old 02-27-2007, 07:01 PM   #2
Registered: Jun 2005
Location: Candia, NH
Distribution: Ubuntu, FC, RHE3, RHE4, CentOS
Posts: 121

Rep: Reputation: 15
A hack would be to find the line number of the cut points and then use 'tail' and 'head':

Say the starting line is 43000 for 40 lines
tail -43000 textfile | head -40 > resultfile

Or use AWK:
#!/usr/bin/awk -f
/--BEGIN_SEGMENT--/ { flag=1; }
if flag==1 { print; }
/--END_SEGMENT--/ { flag=0; }

Then 'chmod +x script; ./script sourcefile > targetfile'
Old 02-28-2007, 04:32 AM   #3
Junior Hacker
Senior Member
Registered: Jan 2005
Location: North America
Distribution: Debian testing Mandriva Ubuntu
Posts: 2,687

Rep: Reputation: 61
To find what line number it starts at:

grep -n BEGIN_SEGMENT txt.filename (from within the file"s directory)

This will show the line number of every instance of BEGIN_SEGMENT

To find what line number it ends at:

grep -n END_SEGMENT txt.filename (again from within the file's directory)
Old 02-28-2007, 09:16 AM   #4
Registered: Jun 2005
Location: Candia, NH
Distribution: Ubuntu, FC, RHE3, RHE4, CentOS
Posts: 121

Rep: Reputation: 15
lothario: How are you making out with this problem?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Which light text editor can copy text from file and paste in browser? davidas Linux - Software 9 03-06-2006 12:28 PM
Reading Large Text File in C++ maheshrajn Programming 6 03-04-2006 09:54 AM
Large text file synapse Slackware 3 02-28-2006 02:21 AM
Parsing large text file with perl smaida Programming 5 09-13-2004 05:33 AM
Script file to replace large text blocks in files? stodge Linux - Software 0 09-27-2003 11:53 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:58 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration