How to extract block from a file?

I have siebel log file as following
EventContext .......
123
.......
SELECT
...
..
EventConext <---- 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
.......

  1. Here all I know is string "ID 16318020: 111.678 seconds". I want to print EventContext line before this string(there may be more occurance of EventContext but I want to print only the first previous occurance from this line.

  2. Also I have need to print block of SQL that start with last SELECT (one before string ID 16318020) and ending with ID 16318020.
    e.g
    SELECT
    Test.....
    ....
    ID 16318020: 111.678 seconds

I am sure some Guru will help me to solve this problem.

Thanks.

This should work:

pattern="16318020: 111.678"
file=siebel.log
awk -v p="$pattern" '
/^EventContext/ { ec=NR }
/^SELECT/ { s=NR }
$0 ~ p {
        c=NR;
        cmd=sprintf("sed -n -e %dp -e %d,%dp %s",ec,s,c,FILENAME);
        system(cmd);
        exit(0);
}
' $file

As usual, replace awk by nawk when relevant.

Brilliant !! You saved a lot of work for me. Thank you so much.

Thanks
Ranjit

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/

I want to know the use of system function in linux and also the difference between system() and backtrick term?

Hi, Mac91.

Welcome to the forum.

Your question will probably not receive much attention because you tacked it onto a thread that has already been answered and has nothing to do with your problem.

I suggest you start a new thread with your question.

You can do that by going to the head of this sub-forum (shell programming-scripting) and clicking the New Thread button ... cheers, drl