sed print between 2 patterns only last occurence


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed print between 2 patterns only last occurence
# 1  
Old 11-02-2012
Power sed print between 2 patterns only last occurence

Hi,

I have a file, which contains the following log data.

Quote:
index sql

*** Index has been created.
*** Total elapsed time was 8 seconds.


+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-

index from 1

*** Index has been created.
*** Total elapsed time was 2 seconds.


+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-
table from 2

*** Index has been created.
*** Total elapsed time was 2 seconds.


+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-
sql create from 1

*** Query completed. One row found. 3 columns returned.
*** Total elapsed time was 1 second.

abc def ghi
--------- ---------- ----------
123 456 789

+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-

.IF ACTIVITYCOUNT > 0 THEN .QUIT 600;
.QUIT 600;
*** You are now logged off from the DBC.
*** Exiting BTEQ...
*** RC (return code) = 600
returncode is 600
I am trying to print fromt he file the following data:

Quote:
abc def ghi
--------- ---------- ----------
123 456 789

+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-
I have tried using sed, but I am getting from the first pattern

Quote:
sed -n '/\*\*\* Total.*\.$/,/^\..*THEN.*600\;$/p' file
Thanks for your help.
# 2  
Old 11-02-2012
I don't know about using sed for this, but the following seems to do what you want using either ed or ex:
Code:
ex -s log <<EOF
?\*\*\* Total?+1,/THEN.*600;/-1p
EOF


Last edited by Don Cragun; 11-03-2012 at 03:21 AM..
# 3  
Old 11-03-2012
Code:
$
$
$ cat -n f16
     1  index sql
     2
     3  *** Index has been created.
     4  *** Total elapsed time was 8 seconds.
     5
     6
     7  +---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-
     8
     9  index from 1
    10
    11  *** Index has been created.
    12  *** Total elapsed time was 2 seconds.
    13
    14
    15  +---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-
    16  table from 2
    17
    18  *** Index has been created.
    19  *** Total elapsed time was 2 seconds.
    20
    21
    22  +---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-
    23  sql create from 1
    24
    25  *** Query completed. One row found. 3 columns returned.
    26  *** Total elapsed time was 1 second.
    27
    28  abc def ghi
    29  --------- ---------- ----------
    30  123 456 789
    31
    32  +---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-
    33
    34  .IF ACTIVITYCOUNT > 0 THEN .QUIT 600;
    35  .QUIT 600;
    36  *** You are now logged off from the DBC.
    37  *** Exiting BTEQ...
    38  *** RC (return code) = 600
    39  returncode is 600
$
$
$ perl -0ne 'while(/Total elapsed.*?\.\n(.*?-\+-)\n/sg){$x=$1} END{print $x,"\n"}' f16

abc def ghi
--------- ---------- ----------
123 456 789

+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-
$
$
$

tyler_durden
# 4  
Old 11-03-2012
With sed only:
Code:
sed -n '/elapsed/,/QUIT/ { 
                    /elapsed/ {n;n;h;n}
                    /QUIT/ {g;p}
                    H
                   } 
        ' file
abc def ghi
--------- ---------- ----------
123 456 789

+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-

# 5  
Old 11-04-2012
Try:
Code:
awk 'c-->0{print A[NR%7]} {A[NR%7]=$0} /^\..*THEN.*600\;$/{c=5}' infile

or

Code:
awk '/^\..*THEN.*600\;$/{printf "%s",p} g{p=p $0 RS} /^\+-/{g=0} /\*\*\* Total.*\.$/{getline; p=x; g=1}' infile


Last edited by Scrutinizer; 11-04-2012 at 02:46 AM..
# 6  
Old 11-04-2012
The sedsolution can be even simplified:
Code:
sed -n '/elapsed/ {n;h;n}
        /THEN .QUIT/ {g;p}
        H
       ' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

2. UNIX for Advanced & Expert Users

Get the first occurence between two patterns

I have an output file which gives me the timely status of a server. Sample file: March 11 2014 21:10, 1, 2, 3, 4, 5, 6, 7, 8, 9, x, y, z... 21:05, 1, 2, 3, 4, 5, 6, 7, 8, 9, x, y, z... 21:00, 1, 2, 3, 4,... (3 Replies)
Discussion started by: rpm120
3 Replies

3. Shell Programming and Scripting

Grab nth occurence in between two patterns using awk or sed

Hi , I have an issue where I want to parse through the output from a file and I want to grab the nth occurrence of text in between two patterns preferably using awk or sed ! TICKET NBR : 1 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2... (8 Replies)
Discussion started by: OTNA
8 Replies

4. Shell Programming and Scripting

Print between patterns - first occurence, second occurence etc

I have a file # cat asasas AAAAAA 11 22 33 44 BBBBB NILNILNIL AAAAAA 22 33 44 55 66 77 88 BBBBB NILNILNIL (2 Replies)
Discussion started by: anil510
2 Replies

5. Shell Programming and Scripting

Print mutliple patterns in a line using sed

Hi, I am trying to print multiple patterns in a line using sed. But it is printing only the last occurance of a pattern. If the line is the the output should be Lookup Procedure|Stored proc But the output I am getting is Stored proc The code I am using is echo... (9 Replies)
Discussion started by: kedar_laveti
9 Replies

6. UNIX for Dummies Questions & Answers

How to print first occurence

Hi there, how can i print the first pattern occurrence in a .log file? I want to print the filename of the first 17262? I tried but all I can do is print all the lines with the number 17262? I tried using awk and sed but nothing!:wall: I just want filename! Here´s an example: 17259... (3 Replies)
Discussion started by: BMatter
3 Replies

7. Shell Programming and Scripting

Retrieve lines that match any occurence in a list of patterns

I have two files. The first containing a header and six columns of data. Example file 1: Number SNP ID dbSNP RS ID Chromosome Result_Call Physical Position 787066 SNP_A-8575395 RS6650104 1 NOCALL 564477 786872 SNP_A-8575125 RS10458597 1 AA ... (13 Replies)
Discussion started by: Selftaught
13 Replies

8. Shell Programming and Scripting

How to get line after occurence of sequence of patterns

In the past I needed a help with the problem how to search for pattern after the occurence of another pattern which is described in this thread: https://www.unix.com/shell-programmin...-pattern1.html Now I would need something quite similar, only the pattern which is to be searched must be... (3 Replies)
Discussion started by: sameucho
3 Replies

9. Shell Programming and Scripting

[Solved] Sed/awk print between patterns the first occurrence

Guys, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla bla 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla ... (6 Replies)
Discussion started by: ppolianidis
6 Replies

10. Shell Programming and Scripting

Sed/awk print between different patterns the first occurrence

Thanks for the help yesterday. I have a little modification today, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR Exception1 blablabla bla bla bla bla 2011-08-14... (2 Replies)
Discussion started by: ppolianidis
2 Replies
Login or Register to Ask a Question