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

  • Home
  • SEARCH
  • 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 8932943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:25:13+00:00 2026-06-15T09:25:13+00:00

I have a file with one or more key:value lines, and I want to

  • 0

I have a file with one or more key:value lines, and I want to pull a key:value out if key=foo. How can I do this?

I can get as far as this:

if File.exist?('/file_name')
  content = open('/file_name').grep(/foo:??/)

I am unsure about the grep portion, and also once I get the content, how do I extract the value?

  • 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-06-15T09:25:14+00:00Added an answer on June 15, 2026 at 9:25 am

    People like to slurp the files into memory, which, if the file will always be small, is a reasonable solution. However, slurping isn’t scalable, and the practice can lead to excessive CPU and I/O waits as content is read.

    Instead, because you could have multiple hits in a file, and you’re comparing the content line-by-line, read it line-by-line. Line I/O is very fast and avoids the scalability problems. Ruby’s File.foreach is the way to go:

    File.foreach('path/to/file') do |li|
      puts $1 if li[/foo:\s*(\w+)/]
    end
    

    Because there are no samples of actual key/value pairs, we’re shooting in the dark for valid regex patterns, but this is the basis for how I’d solve the problem.

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

Sidebar

Related Questions

I have an XML file containing one (or more) key/value pairs. For each of
I have one file (for example: test.txt), this file contains some lines and for
I have added eth0:1 with one more ip to the interface file of my
Hi I have a text file (one word per line) and I want to
I want to create a 'mini-database'. I want to have one file with lots
I have opened one file with following way: fp = fopen(some.txt,r); Now in this
I have one file in my project, (the readme file,) which I would like
I have a file with one line in it. I create a branch and
I have problem with session in cakephp.I have one file chat.php that is in
I have a PHP file with one simple echo function: echo 'アクセスは撥ねりません。'; but when

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.