-   Linux - Software (
-   -   extracting a chunk of text from a large text file (

lothario 02-27-2007 04:35 PM

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.


dgar 02-27-2007 06:01 PM

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'

Junior Hacker 02-28-2007 03:32 AM

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)

dgar 02-28-2007 08:16 AM

lothario: How are you making out with this problem?

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