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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:07:02+00:00 2026-05-24T02:07:02+00:00

I am looking for a safe and reliable way to overwrite ONE line in

  • 0

I am looking for a safe and reliable way to overwrite ONE line in a text file. I don’t care if it’s using sed, grep, perl whatever. It just needs to be portable and reliable. Specifically what I am trying to do is replace the value of a variable I have saved in a text file at runtime. Let’s say I have a file named variables.txt which contains a line that reads userName=stephen. Let’s say my program wants to change the userName to frank. Here’s what I’ve come up with using sed:

sed -i '' 's/userName.*/userName=frank/' variables.txt

The concern I have with this is that I’ve read that on some versions of sed using the ‘-i’ switch without specifying a backup file could cause the command to fail or risk possible file corruption. Not an option. What do you guys think?

Edit

For those asking where I read about command failure and file corruption. The manpage for my version of sed recommends against providing an empty value for the -i switch as well as take a look at the comments on this page here:

It seems that some versions of sed require the argument after -i and others do not. With GNU sed version 4.1.x, it seems that the -i does not require an argument and specifying an empty argument after it actually fails.

It sounds like the unanimous recommendation is to provide a backup file and then delete it after the command completes. However I’m still concerned about this solution since my version of sed doesn’t even support the –version switch. My primary concern here is that the solution is both reliable and portable.

  • 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-24T02:07:03+00:00Added an answer on May 24, 2026 at 2:07 am
    t=`tempfile`
    sed -e 's/userName.*/userName=frank/' variables.txt >$tempfile
    cp $tempfile >variables.txt
    rm $tempfile
    

    you can also use mv but that won’t preserve file rights

    if tempfile is not available then use some other method ($$.bak) to create the filename.

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

Sidebar

Related Questions

I'm looking for a fail-safe way to round-trip between a Flash swf file and
I am looking for a good one way encryption/hashing for safe storage of passwords
We're currently using Visual Source Safe and BugNet and looking to migrate up and
I was recently looking for a way to implement a doubly buffered thread-safe cache
I'm looking for a safe way of building a string of a HTML element
I am looking for a clean and safe way to ensure tha a field
Looking for a way to produce a filename-safe hash of a given PHP array.
I've been looking for a way to do safe vectors and maps of dynamic
I'm looking for a way to determine whether a picture is explicit (is Safe
I'm looking for an easy and safe way to parse a map, and only

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.