Solved

Want to print the last N sections of text between two delimiters using AWK or SED

Posted on 2011-03-11
3
701 Views
Last Modified: 2012-05-11
I have a text file from which I want to extract the last N number of ranges of delimited text.  The number of delimited ranges in this file will always be changing.

Following is an example of the input file.  Note that the ranges I want to extract are delimited by "/== START OF CHECK ==/" and "/== END OF CHECK ==/"   There can be any number of lines with any number of characters per line between delimiters.  

Here's the input file:
/== START OF CHECK ==/
Thu Jun  3 13:28:01 EDT 2010
...
... random number of lines of information
...
/== END OF CHECK ==/

/== START OF CHECK ==/
Thu Jun  3 13:30:01 EDT 2010
...
... random number of lines of information
...
...
/== END OF CHECK ==/

/== START OF CHECK ==/
Fri Mar 11 04:15:01 EST 2011
...
... random number of lines of information
...
/== END OF CHECK ==/

/== START OF CHECK ==/
Fri Mar 11 04:30:01 EST 2011
...
... random number of lines of information
...
/== END OF CHECK ==/

/== START OF CHECK ==/
Fri Mar 11 04:45:01 EST 2011
...
... random number of lines of information
...
/== END OF CHECK ==/

Using the beginning and ending delimiters, how can I use AWK or SED to extract the last N ranges of text?  

Using the example file above, if I wanted the last 3 ranges, I would get:
/== START OF CHECK ==/
Fri Mar 11 04:15:01 EST 2011
...
... random number of lines of information
...
/== END OF CHECK ==/

/== START OF CHECK ==/
Fri Mar 11 04:30:01 EST 2011
...
... random number of lines of information
...
/== END OF CHECK ==/

/== START OF CHECK ==/
Fri Mar 11 04:45:01 EST 2011
...
... random number of lines of information
...
/== END OF CHECK ==/
0
Comment
Question by:rdavis777
  • 2
3 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
Comment Utility
Not very elegant (rather "quick and dirty"):

#!/bin/sh

IN=/path/to/inputfile
NUMSEC=number_of_sections
 
SECB="START OF CHECK"
SECE="END OF CHECK"
START=$(($(grep -c "$SECB" $IN)-$NUMSEC))

sed -n "/$SECB/,/$SECE/p" $IN | \
 awk -v S=$START -v B="$SECB" -v E="$SECE" \
  '{if(n>=S) {if ($0~B) P="Y"; if(P=="Y") print}; if ($0~B) n+=1}'

exit

wmp
0
 
LVL 8

Expert Comment

by:point_pleasant
Comment Utility
here is a perl script to do it

USAGE perl progname < text_file > output_file

$inblock = 0;
while (<>) {
        if ((( $_ =~ /^.* START /) || ($inblock == 1)) && ($_ !~ /^.* END/)) {
                print $_;
                $inblock = 1;
        } elsif ($_ =~ /^.* END/) {
                print $_;
                $inblock = 0;
        }
}
0
 
LVL 8

Expert Comment

by:point_pleasant
Comment Utility
oops didn't read the whole question, here is complete for the last nsection

usage perl paroname.pl input_file number_sections

$inblock = 0;
open (INFILE, "<$ARGV[0]");
open (TMPFILE, ">/tmp/tmpfile");
@file = reverse <INFILE>;
$numsecs=$ARGV[1];
$sec_count = 0;
foreach $line (@file) {
        if ((( $line =~ /^.* END /) || ($inblock == 1)) && ($line !~ /^.* START/)) {
                print TMPFILE $line;
                $inblock = 1;
        } elsif ($line =~ /^.* START/) {
                print TMPFILE $line;
                $inblock = 0;
                if ($sec_count == numsecs){
                        last;
                }else{
                        $sec_count = $sec_count + 1;
                }
        }
}
close (INFILE);
close (TMPFILE);
open (TMPFILE, "</tmp/tmpfile");
@file2 = reverse <TMPFILE>;
foreach $line (@file2) {
        print $line;
}
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now