Grab nth occurence in between two patterns using awk or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grab nth occurence in between two patterns using awk or sed
# 1  
Old 06-19-2013
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



Code:
! 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 !
! TICKET NBR : 2 ! GSI : 102 ! 3100.2.112.1 11/06/2013 16:00:45 ! 3100.2.22.3 65 ! 3100.2.134.2 3 !
! TICKET NBR : 3 ! GSI : 102 ! 3100.2.112.1 11/06/2013 16:00:45 ! 3100.2.22.3 45 ! 3100.2.134.2 6 !

Let's just say for this example I want to grab the second occurrence of text in between TICKET NBR ($count=2)and done, essentially the output would be

Code:
! TICKET NBR : 2 ! GSI : 102 ! 3100.2.112.1 11/06/2013 16:00:45 ! 3100.2.22.3 65 ! 3100.2.134.2 3 !


Last edited by Scrutinizer; 06-19-2013 at 11:36 AM.. Reason: icode tags => code tags
# 2  
Old 06-19-2013
Well, you could always use:-
Code:
grep "TICKET NBR" input_filename | head -2 | tail -1

What further processing are you considering. That may vary the responses you get.



I hope that this helps,

Robin
Liverpool/Blackburn
UK
# 3  
Old 06-19-2013
please give any thing except this , already tried this and i dont know why i got this error , there is nothing related to connection i'm already working on the same server , its working fine many times but suddenly i got this error

grep: writing output: Connection reset by peer

i need to try this in another way may be SED or AWK
# 4  
Old 06-19-2013
Then try this:
Code:
awk '/TICKET NBR/ && ++i==2' file


Last edited by Subbeh; 06-19-2013 at 11:46 AM..
This User Gave Thanks to Subbeh For This Post:
# 5  
Old 06-19-2013
Hi ,

its working fine but sorry im newbe in AWK , please how to make it read from count ,
i'm already have acounter lets say

Code:
nbrTickets =100
while [ $count -le $nbrTickets ]
do
awk '/TICKET NBR/{Here put count print}' file > $tkt

Moderator's Comments:
Mod Comment Please use code tags instead of icode tags

Last edited by Scrutinizer; 06-19-2013 at 12:12 PM.. Reason: code tags instead of icode tags
# 6  
Old 06-19-2013
What do you want to achieve? Get the first 100 occurrences of 'TICKET NBR'?
# 7  
Old 06-19-2013
the script is working fine , i just want to get one by one , for example get the first one and do some editing then get the second one and so on
how to put this in a loop
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get nth occurence of string from a file

I have file in which the data looks like this, 01,0000000,xxxxxxx/ 02,xxxxxxxx,yyyyyy/ 03,test1,41203016,,/ 01,0000000,xxxxxxx/ 02,xxxxxxxx,yyyyyy/ ... (16 Replies)
Discussion started by: r@v!7*7@
16 Replies

2. UNIX for Dummies Questions & Answers

Extract until nth occurence

Hi, I couldn't figure how to extract until last occurence of a character. I have the string ./dir1/file1/abc.sh The output should be /dir1/file1 So, the command should display the path until last occurence of "/". Thanks. (3 Replies)
Discussion started by: rajivn786
3 Replies

3. Shell Programming and Scripting

Grab contents between two patterns

Hi, What is the best approach to grab contents between Changes Dependencies from the following example snippy Changes in packages about to be updated: bash-3.2-32.el5_9.1.x86_64 * Thu Jun 27 22:00:00 2013 Roman Rakus <rrakus@redhat.com> - 3.2-32.1 - Fixed a bug that caused... (2 Replies)
Discussion started by: ashokvpp
2 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

Delete until Nth occurence (sed, awk)

Hello people, Once more I need your help with SED/AWK I need to delete up to the Nth occurence of a char (from the beggining) and until the Mth occurence of a char (from the end) Example: Input: a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z Output: i,j Must delete up to the... (2 Replies)
Discussion started by: drbiloukos
2 Replies

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

7. UNIX for Dummies Questions & Answers

To find the Nth Occurence of Search String

Hi guys, I like to find the Line number of Nth Occurence of a Search string in a file. If possible, if it will land the cursor to that particualar line will be great. Cheers!! (3 Replies)
Discussion started by: mac4rfree
3 Replies

8. UNIX for Dummies Questions & Answers

Grab Portion of Output Text (sed, grep, awk?)

Alright, here's the deal. I'm running the following ruby script (output follows): >> /Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c Clouds AND Sun 57/33 - Mostly sunny and cool I want to just grab the "57/33" portion, but that's it. I don't want any other portion of the line. I... (5 Replies)
Discussion started by: compulsiveguile
5 Replies

9. Shell Programming and Scripting

Replacing nth occurence

Hi My input file is like this for eg: abc abc abc abc abc abc i would like to replace "abc" with "cba" where the occurrence is divisible by 2 of eg here 2nd, 4th and 6th occurence shud be replace can anyone suggest in awk or sed (11 Replies)
Discussion started by: raghav288
11 Replies

10. Shell Programming and Scripting

Replace matching nth occurence

Hi for eg my file has: val1 | val2 | val1 | val2 | val1 | val2 | val1 | val2 | here i need to replace '|' with '|\n' where the occurence of '|' is divisble by 2 so that the output comes like this val1 | val2 | val1 | val2 | val1 | val2 | val1 | val2 | Requesting suggestions in... (1 Reply)
Discussion started by: raghav288
1 Replies
Login or Register to Ask a Question