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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:00:15+00:00 2026-06-07T23:00:15+00:00

I need to search for certain lines in a file that contain numbers, then

  • 0

I need to search for certain lines in a file that contain numbers, then do some manipulation on those numbers before replacing them in the file. For example, say I’ve got a line like this:

normal: { x: 50, y: 50 }

I need to find that line, divide both numbers by 2, then replace the numbers in the line and update the file. I have no problem writing the regex to find the line, and have written a little PHP script using preg_replace_callback() to manipulate the numbers, but I need to be able to replace the numbers then update just that line in the file. I’d be happy to use bash/sed/awk or Ruby or Python or PHP, but I’m not an expert on any of those.

Should I just read the whole file in, do my replace_callbacks then overwrite the 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-06-07T23:00:18+00:00Added an answer on June 7, 2026 at 11:00 pm

    Yes. What you suggested is the best option.
    Read the whole file, replace the numbers (you already did that), and write the whole file back.

    In case you have a really huge file, you might run into trouble because it won’t fit into memory.
    You can of course, scan the file, and only replace what you need to replace.
    If you are going to fiddle with the filehandle, and overwrite only the bytes you need to overwrite (and leave the remainder of the file alone) you might find yourself in situations like:

    normal: { x: 10, y: 6 }
    

    that become

    normal: { x: 5, y: 3 }
    

    and thus you lost a byte because “10” becomes “5”. 🙂
    In your example you can use a space instead, but I wouldn’t know if that would work in all situations you will encounter.

    In short: do what you proposed yourself: read, manipulate, and write back.

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

Sidebar

Related Questions

I have a text file that I need to search and then print to
I have a need to search a pdf file to see if a certain
I need to search for a certain process and kill that process. I wrote
I need to search Facebook for certain keywords and count them if possible? Thanks!
I need to search for certain users on Facebook by their names(ex. Full name
I need to search for all users containing a certain text string in their
I need to write an AS3 program to search for a certain keyword in
i need to pull data out of an xml file based on certain field
I need to display ABPeoplePickerNavigationController according to a certain search criteria. I pass a
I have a really huge log file from which i need to search stuff.

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.