?
Solved

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

Posted on 2011-03-11
3
Medium Priority
?
725 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 35109782
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
ID: 35110511
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
ID: 35110800
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 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