Get the first occurence between two patterns


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Get the first occurence between two patterns
# 1  
Old 03-11-2014
Lightbulb Get the first occurence between two patterns

I have an output file which gives me the timely status of a server.

Sample file:

Code:
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,    5,    6,    7,    8,    9,    x,    y,    z...
20:55,    1,    2,    3,    4,    5,    6,    7,    8,    9,    x,    y,    z...
20:50,    1,    2,    3,    4,    5,    6,    7,    8,    9,    x,    y,    z...
.
.
.
00:00,    1,    2,    3,    4,    5,    6,    7,    8,    9,    x,    y,    z...

March 10 2014

21:10,    11,    22,    33,    44,    55,    66,    77,    88,    99,    xx,    yy,    zz...
21:05,    11,    22,    33,    44,    55,    66,    77,    88,    99,    xx,    yy,    zz...
21:00,    11,    22,    33,    44,    55,    66,    77,    88,    99,    xx,    yy,    zz...
20:55,    11,    22,    33,    44,    55,    66,    77,    88,    99,    xx,    yy,    zz...
20:50,    11,    22,    33,    44,    55,    66,    77,    88,    99,    xx,    yy,    zz...
.
.
.
00:00,    11,    22,    33,    44,    55,    66,    77,    88,    99,    xx,    yy,    zz...

March 09 2014

21:10,    123,    221,    331,    441,    551,    661,    771,    881,    991,    xxx,    yyy,    zzz...
21:05,    123,    221,    331,    441,    551,    661,    771,    881,    991,    xxx,    yyy,    zzz...
21:00,    123,    221,    331,    441,    551,    661,    771,    881,    991,    xxx,    yyy,    zzz...
20:55,    123,    221,    331,    441,    551,    661,    771,    881,    991,    xxx,    yyy,    zzz...
20:50,    123,    221,    331,    441,    551,    661,    771,    881,    991,    xxx,    yyy,    zzz...
.
.
.
00:00,    123,    221,    331,    441,    551,    661,    771,    881,    991,    xxx,    yyy,    zzz...

I need to get the first occurrence or sequence between the two patterns i.e "21:10" & "00:00" (time stamps)


Desired output


Code:
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,    5,    6,    7,    8,    9,    x,    y,    z...
20:55,    1,    2,    3,    4,    5,    6,    7,    8,    9,    x,    y,    z...
20:50,    1,    2,    3,    4,    5,    6,    7,    8,    9,    x,    y,    z...
.
.
.
00:00,    1,    2,    3,    4,    5,    6,    7,    8,    9,    x,    y,    z...

Hope I m clear with the problem statement .... Please help...
# 2  
Old 03-11-2014
Like this?
Code:
awk '/21:10/,/00:00/{p=1} p && /^ *$/{exit} p' infile

Always try to understand the code everyone in this forum provides, it will help you next time.
# 3  
Old 03-11-2014
Code:
ex -sc "/^21:10/,/^00:00/|q!" file

# 4  
Old 03-11-2014
Code:
awk '/21:10/,/00:00/;/00:00/{exit}' file

--
Different approach, printing the first block, irrespective of time stamp:
Code:
awk 'NF>4{print; exit}' RS= file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - Find files excluding file patterns and subfolder patterns

Hello. For a given folder, I want to select any files find $PATH1 -f \( -name "*" but omit any files like pattern name ! -iname "*.jpg" ! -iname "*.xsession*" ..... \) and also omit any subfolder like pattern name -type d \( -name "/etc/gconf/gconf.*" -o -name "*cache*" -o -name "*Cache*" -o... (2 Replies)
Discussion started by: jcdole
2 Replies

2. 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

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

sed print between 2 patterns only last occurence

Hi, I have a file, which contains the following log data. I am trying to print fromt he file the following data: I have tried using sed, but I am getting from the first pattern Thanks for your help. (5 Replies)
Discussion started by: sol_nov
5 Replies

6. 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

7. 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

8. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

9. UNIX for Dummies Questions & Answers

First occurence from grep

Hi , supoose i have a file in which a word is repeated so many times. I just want the firts occurence of that word through grep and it should not go to the next one means get the first occurence and stop there. Suggest me some solutions. Thanks Namish (10 Replies)
Discussion started by: namishtiwari
10 Replies

10. UNIX for Advanced & Expert Users

First Occurence

Hi, This is the format of the file that i have StartDate:10/01/06 EndDate :10/02/06 Cno Ccode 1 10 2 11 StartDate:10/03/06 EndDate :10/04/06 Cno Ccode 2 13 4 12 StartDate:10/01/06 EndDate :10/02/06 (5 Replies)
Discussion started by: kkm_job
5 Replies
Login or Register to Ask a Question