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 6213285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:40:10+00:00 2026-05-24T06:40:10+00:00

I am new to programming please help me. I need to read a file

  • 0

I am new to programming please help me.
I need to read a file from particular line based on the time and write the same into another file. But its skipping the first line while writing into the other file.

timeStr="2011-08-01 02:24"
File.open(path+ "\\logs\\messages.log", "r") do |f| 
  # Skip the garbage before pattern: 
  while f.gets !~ (/#{timeStr}/) do; end                   
  # Read your data: 
  while l = f.readlines
    File.open(path+ "\\logs\\messages1.log","a") do |file1|
      file1.puts(l)
    end
  end 
end

When am running the above script the first line matching timeStr is skipped and from the second line the file is written in to the messages1. when I open messages1.log file the first line containing the matching string will not be present. Any idea how to include the first line also while writing to the messages1.log file.

  • 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-24T06:40:10+00:00Added an answer on May 24, 2026 at 6:40 am

    I think you want to keep the line that matches /#{timeStr}/ but this loop:

    while f.gets !~ (/#{timeStr}/) do; end
    

    throws it away. You could rearrange things a bit:

    # Get `line` in the right scope.
    line = nil
    
    # Eat up `f` until we find the line we're looking for
    # but keep track of `line` for use below.
    while(line = f.gets)
        break if(line =~ /#{timeStr}/)
    end
    
    # If we found the line we're looking for then get to work...
    if(line)
        # Grab the rest of the file
        the_rest = f.readlines
        # Prepend the matching line to the rest of the file
        the_rest.unshift(line)
        # And write it out.
        File.open(path + "\\logs\\messages1.log","a") do |file1|
            file1.puts(the_rest)
        end
    end
    

    I haven’t tested this but it should work baring typos and the like.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am new at programming and i need some help with that please =/
I am quite new to programming, i have a question please help me. (
Im new to programming..Can anybody help me out please .... im using an html
I'm fairly new to C# programming and need some help. I am trying to
I am new to programming in general so please keep that in mind when
I'm new to the world of Mac programming. Can someone please tell me what
i am new to iPhone programming.. i have an html file..in that 3 lines
I am new to programming, so please accept my apologies if this question is
I'm kind of new to this site and programming in general, so please excuse
I am quite new to programming and will really appreciate if anyone can help

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.