Home > OS >  How to get the line number of a pattern in R
How to get the line number of a pattern in R

Time:10-17

I have a html file. I am trying find all line number of a pattern "<table". So here I have:

line_num = grep("<table", html_file)
line_num

The output gives me the lines that contain pattern "<table":

## [1] 199 217 219

However, my problem is that in line 217, the pattern "<table" has appeared twice. Therefore I want my output to look like:

## [1] 199 217 217 219

So it prints line number for every appearance of the pattern "<table". How do I do this? TIA!

CodePudding user response:

You may use str_count to count the number of times a pattern occurs, drop the 0 counts and repeat the line_num count number of times.

line_num <- grep("<table", html_file)
line_count <- stringr::str_count(html_file, "<table")
line_count <- line_count[line_count != 0]
rep(line_num, line_count)
  • Related