INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Search for a key and replace the value in a file

Search for a key and replace the value in a file

(OP)
Hi,

I have 2 files as below.

file1.txt
=========
var1=value1
var2=value2


file2.txt
==========
var1=xxxx
var2=yyyy
var3=zzzzz

I need to look for each key from file1.txt in file2.txt and then replace the value of that key in file2.txt with the value that is in file1.txt. The final result should be as below.
==========
file2.txt
==========
var1=value1
var2=value2
var3=zzzzz

Here is the code that I have. The following "sed" command works fine from command line but not inside the script. Please let me know if I'm doing anything wrong.
===============
#!/bin/sh
cat "file1.txt" | while IFS== read key value
do
echo "key='$key' value='$value'"
sed -i '/^${key}=/s/=.*/=${value}/' file2.txt
done

RE: Search for a key and replace the value in a file

(OP)
Never mind it is working after putting double quotes in the sed command

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close