Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for a key and replace the value in a file

Status
Not open for further replies.

tulsantide

Technical User
Jun 1, 2015
21
US
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
 
Never mind it is working after putting double quotes in the sed command
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top