Print between patterns - first occurence, second occurence etc


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print between patterns - first occurence, second occurence etc
# 1  
Old 06-18-2013
Print between patterns - first occurence, second occurence etc

I have a file
Code:
# cat asasas
AAAAAA
11
22
33
44
BBBBB
NILNILNIL
AAAAAA
22
33
44
55
66
77
88
BBBBB
NILNILNIL
AAAAAA
00
33
1212
00
BBBBB
NILNILNIL

I need to print between patterns AAAAAA and BBBBB. It works fine with
Code:
# cat asasas | sed -n '/AAAAAA/,/BBBBB/p'
AAAAAA
11
22
33
44
BBBBB
AAAAAA
22
33
44
55
66
77
88
BBBBB
AAAAAA
00
33
1212
00
BBBBB

What I need is, print first occurrence, second occurrence and third occurrence

ie
Code:
cat  asasas | sed_operation
AAAAAA
11
22
33
44
BBBBB

Code:
cat  asasas | sed_operation
AAAAAA
22
33
44
55
66
77
88
BBBBB

Code:
cat  asasas | sed_operation
AAAAAA
00
33
1212
00
BBBBB

# 2  
Old 06-18-2013
Try with awk..

Code:
awk '/AAAAAA/{f=1}
    {S=S?S "\n" $0 : $0}
    f && /BBBBB/ {print S;S=""}' file

# 3  
Old 06-18-2013
Change the value of p to print pattern number you like eks p==3 to print block 3
Code:
awk '/AAAAAA/ {p++} /AAAAAA/ && p==3 {f=1} /BBBBB/ && f {f=0;print;exit} f' file
AAAAAA
00
33
1212
00
BBBBB


Code:
awk '
	/AAAAAA/		{p++}
	/AAAAAA/ && p==3	{f=1}
	/BBBBB/ && f		{f=0;print;exit}
	f
	' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print occurence number

Hi folks, I have a file with lots of lines in a text file,i need to print the occurence number after sorting based on the first column as shown below, thanks in advance. sam,dallas,20174 sam,houston,20175 sam,atlanta,20176 jack,raleigh,457865 jack,dc,7845 john,sacramento,4567 ... (4 Replies)
Discussion started by: tech_frk
4 Replies

2. Shell Programming and Scripting

Help with using awk to print pattern/occurence

Hi, Do anybody know how to use awk to count the pattern at specific column? Input file M2A928K 419 ath-miR159a,gma-miR159a-3p,ptc-miR159a 60 miR235a . . Output file M2A928K 419 ath-miR159a,gma-miR159a-3p,ptc-miR159a 60 miR235a 3 . . I plan to count how many "miR" in column 3... (2 Replies)
Discussion started by: cpp_beginner
2 Replies

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

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

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

HOW TO - Bash REGEX - Print Error when More then One Occurence is Found Consecutively?

Hello All, Bash Version: 4.1.10(1) I'm trying to "verify" some user input. The User input will contain the "Absolute Path" a "Command" and any "Options" of that Command. For Example, say the user's input is: user_input="/usr//local/myExample/check_process -p 'java' -w 10 -c 20" I... (6 Replies)
Discussion started by: mrm5102
6 Replies

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

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

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

10. Shell Programming and Scripting

Print row if value in column 1 is the first occurence

Hi All, I would like to have a script which is able to perform the below. Print the whole row if column1 which is "0001" for the below example is the first occurrence. Subsequent "0001" occurrence will not be printed out and so on. Can any expert help ? Input: 0001 k= 40 0001 k= 2... (7 Replies)
Discussion started by: Raynon
7 Replies
Login or Register to Ask a Question