line number of the i-th occurrence of a pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers line number of the i-th occurrence of a pattern
# 1  
Old 12-16-2009
line number of the i-th occurrence of a pattern

Hi all,
is there a simple way to obtain the line number of the i-th occurrence of a pattern?

I have

Code:
OCCURRENCE=`grep -io "${STRING_NAME[$J]}" ${1}-${8}${EXT}.out_bis| wc -l`

which tells me how many occurency I have. I would like to go through them and determine the line number and assign it to a variable.
I don't want to have them in an array but in a signle variable.

Any suggestion?
Thank you,
Sarah
# 2  
Old 12-16-2009
Not shure if this is what you're looking for but this is an example to print the line number of the 3th occurrence of the pattern ${STRING_NAME[$J]} of the file ${1}-${8}${EXT}.out_bis:

Code:
awk -v pattern="${STRING_NAME[$J]}" -v occ="3" '
$0 ~ pattern{i++}
i==occ{print NR;exit}
' ${1}-${8}${EXT}.out_bis


Last edited by Franklin52; 12-16-2009 at 11:36 AM..
# 3  
Old 12-16-2009
Code:
gawk '{for(i=1;i<=NF;i++) if($i=="word"){++ith} }ith==2{print NR;exit}' file

# 4  
Old 12-16-2009
Thank you,
I came up with

Code:
            var1=${STRING_NAME[$J]}
            file=${1}-${8}${EXT}.out_bis
            ST_LINE=`awk "/$var1/ && (++x==$i){ print NR}"  $file`

which I used in the past.
but I get the followoing message

Code:
awk: /blah blah blah blah/blah/basis/ && (++x==0){ print NR}
awk:                                             ^ syntax error

I don't what I'm doing wrong...I think the problem is the / in var1.

Last edited by f_o_555; 12-16-2009 at 11:30 AM..
# 5  
Old 12-16-2009
Try this:
Code:
ST_LINE=`awk -v var1=${STRING_NAME[$J]} -v i="3" '$0 ~ var1 && --i==0 {print NR}' $file`

# 6  
Old 12-16-2009
Quote:
Originally Posted by f_o_555
Hi all,
is there a simple way to obtain the line number of the i-th occurrence of a pattern?

I have

Code:
OCCURRENCE=`grep -io "${STRING_NAME[$J]}" ${1}-${8}${EXT}.out_bis| wc -l`

which tells me how many occurency I have. I would like to go through them and determine the line number and assign it to a variable.
I don't want to have them in an array but in a signle variable.

Any suggestion?
Thank you,
Sarah
Hi Sarah,
you can try this
Code:
last_line_of_occ=$(grep -i -n pattern file_txt | tail -1 | awk -F":" '{print $1}')

for ith occurrance you can modify the awk like this
Code:
ith_line=$(grep -i -n java exam_schedule.txt | awk -F":" 'NR==i{print $1})'

where substitute i as the no of occurrances you want . eg. 5 for 5th occurrance will provide the line no. of 5th occurrance.
Hope this helps.
Regards.
# 7  
Old 12-17-2009
Thank you all,
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to extract and print first occurrence of pattern in each line

I am trying to use awk to extract and print the first ocurrence of NM_ and NP_ with a : before in each line. The input file is tab-delimeted, but the output does not need to be. The below does execute but prints all the lines in the file not just the patterns. Thank you :). file tab-delimeted ... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Remove matching pattern on each line with number variations

Hello folks! I have a file containing lines like this Something text 18:37Remove This: 1,111"Keep this text" Some more text 19:37Remove This: 222"Keep this text" More text 20:50Remove This: 3,333Keep this text And more text 25:50Remove This: 44,444Keep this text I would like to... (4 Replies)
Discussion started by: martinsmith
4 Replies

3. Shell Programming and Scripting

Insert content of file before the first occurrence of a line starts with a pattern in another file

Hi all, I'm new to scripting.. facing some problems while inserting content of a file into another file... I want to insert content of a file (file2) into file1, before first occurrence of "line starts with pattern" in file1 file1 ====== working on linux its unix world working on... (14 Replies)
Discussion started by: Jagadeesh Kumar
14 Replies

4. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

5. Shell Programming and Scripting

Count number of occurrence at each line

Hi I have the following file ENST001 ENST002 4 4 4 88 9 9 ENST004 3 3 3 99 8 8 ENST009 ENST010 ENST006 8 8 8 77 8 8 Basically I want to count how many times ENST* is repeated in each line so the expected results is 2 1 3 Any suggestion please ? (4 Replies)
Discussion started by: fuad_
4 Replies

6. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

7. UNIX for Dummies Questions & Answers

Line number of an occurrence using grep

Hi All, is there a way to extract the line number of an occurrence using grep? I know that with the -n option it prints out the line number as well. I would like to assign the line number to a variable. Thanks, Sarah (5 Replies)
Discussion started by: f_o_555
5 Replies

8. Shell Programming and Scripting

Count the number of occurrences of a pattern between each occurrence of a different pattern

I need to count the number of occurrences of a pattern, say 'key', between each occurrence of a different pattern, say 'lu'. Here's a portion of the text I'm trying to parse: lu S1234L_149_m1_vg.6, part-att 1, vdp-att 1 p-reserver IID 0xdb registrations: key 4156 4353 0000 0000 ... (3 Replies)
Discussion started by: slipstream
3 Replies

9. Shell Programming and Scripting

how to find the line number of a pattern of first appearance??

Hi, I am doin a project in shell script please answer the above question..... waiting........ (2 Replies)
Discussion started by: shivarajM
2 Replies

10. Shell Programming and Scripting

Split File Based on Line Number Pattern

Hello all. Sorry, I know this question is similar to many others, but I just can seem to put together exactly what I need. My file is tab delimitted and contains approximately 1 million rows. I would like to send lines 1,4,& 7 to a file. Lines 2, 5, & 8 to a second file. Lines 3, 6, & 9 to... (11 Replies)
Discussion started by: shankster
11 Replies
Login or Register to Ask a Question