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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:47:42+00:00 2026-06-08T00:47:42+00:00

I’d like to replace one string from one file with a string from another

  • 0

I’d like to replace one string from one file with a string from another file. Though I’m not experienced with these commands, I expect some combination of grep and sed would do it best.

What makes this a bit more complicated is that I don’t know what either string is (I’m trying to automate replacing the version number on my documentation). I do know that in both cases the string I’m looking for (say “2.3.4”) is preceded by “version:”

So can I say ‘look for word (or rest of line or whatever is possible) after “version:” (let’s call it string1) and do the same in another file (giving string2) and replace string string1 with string2.

Here are some example text files:

file1.txt

This is a file containing
the updated version number.
version: 2.3.4
here is a string with more info

file2.txt

This is a configuration file
It could contain an old version number
version: 2.3.2
Please update this

So the expected output for file2.txt would become:

file2.txt

This is a configuration file
It could contain an old version number
version: 2.3.4
Please update this

Thanks

  • 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-08T00:47:44+00:00Added an answer on June 8, 2026 at 12:47 am

    Provided you have a sed which supports the -i option,

    sed -i 's/version: .*/version: 1.2.3/' file1 file2 file3 ...
    

    You may want to tweak the regex wildcard; .* matches through the end of the line, whereas [.0-9]* matches the longest possible sequence of dots and digits. You might also want to permit for variations in surrounding whitespace … But since this is probably among the top 10% FAQs on this site, go look for similar questions at this point.

    To obtain the replacement string from file1 and apply it to file2, file3, etc, something like

    new=$(sed -n 's/version: //p' file1)
    # Use double quotes, not single, in order to expand $new
    sed -i "s/version: [.0-9]*/version: $new/" file2 file3 ...
    

    The first sed invocation will only print lines on which “version: ” was found and removed (replaced with an empty string). Presumably there will only be one such line in the file. Pipe the output to head -n 1 or uniq or something, or find / create a more elaborate sed script.

    You normally use single quotes around literal strings, but since you don’t want a literal $new in the replacement, we use double quotes, which allow the shell to perform variable replacement (and a number of other substitutions we don’t go into here) in the quoted string.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I would like to count the length of a string with PHP. The string
I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.