Solved

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

Posted on 2011-03-11
3
712 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 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWK: Pythagoras bp script 18 47
Granting root permissions to a user (in Debian Jessie) 17 76
Enable SPF on IMSVA 6 62
How to check if the Cron job is running or not ? 12 72
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses

740 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