Hi.
Here is an alternate solution. The nonstandard cgrep allows one to specify the edges of a window around a regular expression. The window edges may themselves be specified as regular expressions. So we can tell cgrep to look for the line matching the last line of interest, and then go backwards to find the preceding line of interest.
Then the sub-block needs to be deleted except for the first and last lines -- again a set of bounds or edges.
In the middle of this script is the call to cgrep, piped into a short perl script to do the sub-block work:
#!/usr/bin/env bash
# @(#) s2 Demonstrate block extraction, deletion with cgrep, perl.
# Section 1, setup, pre-solution.
# Infrastructure details, environment, commands for forum posts.
# Uncomment export command to test script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
set +o nounset
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
C=$HOME/bin/context && [ -f $C ] && . $C specimen cgrep perl
set -o nounset
pe
FILE=${1-data1}
# Section 2, display input file.
# Display sample of data file, with head & tail as a last resort.
pe " || start [ first:middle:last ]"
specimen 8 $FILE \
|| { pe "(head/tail)"; head -n 5 $FILE; pe " ||"; tail -n 5 $FILE; }
pe " || end"
# Section 3, solution.
pl " Results (see file t1 for intermediate results):"
cgrep -D -w "EventContext" 'ID 16318020: 111.678 seconds' $FILE |
tee t1 |
perl -e '
while (<>) {
# Check if we are in desired range.
if ( $line = /EventContext/ .. /SELECT/ ) {
if ( $line =~ /E0/ ) {
print $_;
}
elsif ( $line == 1 ) {
print $_;
} # Otherwise, delete by implication
next;
}
# Print everything else.
print;
}
'
exit 0
which, when run, produces:
% ./s2
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0 (lenny)
GNU bash 3.2.39
specimen (local) 1.17
cgrep - (local: ~/executable/cgrep May 29 2009 )
perl 5.10.0
|| start [ first:middle:last ]
Whole: 8:0:8 of 15 lines in file "data1"
EventContext (one) .......
123
.......
SELECT
...
..
EventContext (two) <---- Question 1 , I should get this line
345
......
SELECT <----- Question 2 , print this line
Test..... <----- Question 2 , print this line
.... <----- Question 2 , print this line
ID 16318020: 111.678 seconds <----- Question 2 , print this line
SELECT
.......
|| end
-----
Results (see file t1 for intermediate results):
EventContext (two) <---- Question 1 , I should get this line
SELECT <----- Question 2 , print this line
Test..... <----- Question 2 , print this line
.... <----- Question 2 , print this line
ID 16318020: 111.678 seconds <----- Question 2 , print this line
As it turns out, extracting and deleting blocks is a common task, so one might create a script to do that based on parameters.
Changing the operative lines to do that in a script s3:
cgrep -D -w "EventContext" 'ID 16318020: 111.678 seconds' $FILE |
tee t1 |
debb "EventContext" "SELECT" d tt
produces:
% ./s3
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0 (lenny)
GNU bash 3.2.39
specimen (local) 1.17
cgrep - (local: ~/executable/cgrep May 29 2009 )
debb (local) 1.15
|| start [ first:middle:last ]
Whole: 8:0:8 of 15 lines in file "data1"
EventContext (one) .......
123
.......
SELECT
...
..
EventContext (two) <---- Question 1 , I should get this line
345
......
SELECT <----- Question 2 , print this line
Test..... <----- Question 2 , print this line
.... <----- Question 2 , print this line
ID 16318020: 111.678 seconds <----- Question 2 , print this line
SELECT
.......
|| end
-----
Results (see file t1 for intermediate results):
EventContext (two) <---- Question 1 , I should get this line
SELECT <----- Question 2 , print this line
Test..... <----- Question 2 , print this line
.... <----- Question 2 , print this line
ID 16318020: 111.678 seconds <----- Question 2 , print this line
Best wishes ... cheers, drl
The source for cgrep can be found at: http://sourceforge.net/projects/cgrep/