Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 891809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:56:55+00:00 2026-05-15T13:56:55+00:00

I’m creating some basic work assistance utilities using Ruby. I’ve hit a problem that

  • 0

I’m creating some basic work assistance utilities using Ruby. I’ve hit a problem that I don’t really need to solve, but curiosity has the best of me.

What I would like to be able to do is search the contents of a file, starting from a particular line and find the first PREVIOUS occurrence of a string.

For example, if I have the following text saved in a file, I would like to be able to search for “CREATE PROCEDURE” starting at line 4 and have this return/output “CREATE PROCEDURE sp_MERGE_TABLE”

CREATE PROCEDURE sp_MERGE_TABLE
AS
 SOME HORRIBLE STATEMENT
 HERE

CREATE PROCEDURE sp_SOMETHING_ELSE
AS
 A DIFFERENT STATEMENT
 HERE

Searching for content isn’t a challenge, but specifying a starting line – no idea. And then searching backwards… well…

Any help at all appreciated!

TIA!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:56:56+00:00Added an answer on May 15, 2026 at 1:56 pm

    Edit:

    I just had a much better idea, but I’m going to include the old solution anyway.

    The benefit of searching backwards means you only have to read the first chunk of the file, upto the specified line number. For proximity, you get closer and closer to the start_line, and if you find a match you just forget the old one.. You still read in some redundant data at the beginning, but at least it’s O(n)

    path = "path/to/file"
    start_line = 20
    search_string = "findme!"
    
    #assuming file is at least start_line lines long
    match_index = nil
    f = File.new(path)
    start_line.times do |i|
       line = f.readline
       match_index = i if line.include? search_string
    end
    
    puts "Matched #{search_string} on line #{match_index}"
    

    Of course, bear in mind that the size of this file plays an important role in answering your question.

    If you wanted to get really serious, you could look into the IO class – it seems like this might be the ultimate solution. Untested, just a thought.

    f = File.new(path)
    start_line.downto(0) do |i|
      f.lineno = i
      break if f.gets.include?(search_string)
    end
    

    Original:

    For an exhaustive solution, you could try something like the following. The downside is you’d need to read the whole file into memory, but it takes into account continuing from the bottom-up if it gets to the top without a match. Untested.

    path = "path/to/file"
    start_line = 20
    search_string = "findme!"
    
    #get lines of the file into an array (chomp optional)
    lines = File.readlines(path).map(&:chomp)
    
    #"cut" the deck, as with playing cards, so start_line is first in the array
    lines = lines.slice!(start_line..lines.length) + lines
    
    #searching backwards can just be searching a reversed array forwards
    lines.reverse!
    
    #search through the reversed-array, for the first occurence
    reverse_occurence = nil
    lines.each_with_index do |line,index|
      if line.include?(search_string)
        reverse_occurence = index
        break
      end
    end
    
    #reverse_occurence is now either "nil" for no match, or a reversed-index
    #also un-cut the array when calculating the index
    if reverse_occurence
       occurence = lines.size - reverse_occurence - 1 + start_line
       line = lines[reverse_occurence]
       puts "Matched #{search_string} on line #{occurence}"
       puts line
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 468k
  • Answers 468k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You may need to explicitly require the post_commit before use… May 16, 2026 at 2:24 am
  • Editorial Team
    Editorial Team added an answer Your X and Y columns are swapped (really, go look… May 16, 2026 at 2:24 am
  • Editorial Team
    Editorial Team added an answer def repeat_to_length(string_to_expand, length): return (string_to_expand * ((length/len(string_to_expand))+1))[:length] For python3: def… May 16, 2026 at 2:24 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.