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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:26:36+00:00 2026-06-12T07:26:36+00:00

Is there a way to edit each line in a file, without involving 2

  • 0

Is there a way to edit each line in a file, without involving 2 files? Say, the original file has,

test01
test02
test03

I want to edit it like

test01,a
test02,a
test03,a

Tried something as show in the code block, but it replaces some of the characters.

Writing it to a temporary file and then replace the original file works, However, I need to edit the file quite often and therefore prefer to do it within the file itself .Any pointers are appreciated.

Thank you!

File.open('mytest.csv', 'r+') do |file|
  file.each_line do |line|        
      file.seek(-line.length, IO::SEEK_CUR)
      file.puts 'a'        
  end
end
  • 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-12T07:26:37+00:00Added an answer on June 12, 2026 at 7:26 am
    f = open 'mytest.csv', 'r+'
    r = f.readlines.map { |e| e.strip << ',a' }
    f.rewind
    f.puts r
    f.close # you can leave out this line if it's the last one that runs
    

    Here is a one-liner variation, note that in this case 2 descriptors are left open until the program exits.

    open(F='mytest.csv','r+').puts open(F,'r').readlines.map{|e|e.strip<<',a'}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In IIS 7 is there a way to edit the forms authentication portion of
Out of curiousity, is there any way to edit an existing synonym? That is,
Is there any possible way to edit this code <asp:LoginName ID=LoginName1 runat=server style=top: 165px;
Is there a best way to edit CSS? I'm looking for a designer tool.
Is there a way to make a click-to-edit control in silverlight? I've got some
Is there a way to display and edit values in the PropertyGrid (and his
Is there a way to know if TAdoQuery in Edit state was changed? Assuming
Is there a way I can apply '+ to '( 1 2 3)? edit:
Is there a way to remove duplicate (key-value) pairs from NSDictionary ? EDIT: My
In other words, is there a way to rewind it to the beginning? EDIT

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.