Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ignore other .htaccess 2 59
What is linux 4.8.22? Is it the same as ubuntu 16.10 ?? 2 56
How to mount nfs share on this CentOS server? 6 43
Redhat upgrade 1 27
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…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

821 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