LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-27-2007, 05:35 PM   #1
lothario
Member
 
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----"
and
ends with the text "----END_SEGMENT----"


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


So that I can extract exactly what I need.

Thanks.
 
Old 02-27-2007, 07:01 PM   #2
dgar
Member
 
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
dgar
Member
 
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?
 
  


Reply


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:22 AM.

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