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!

Replace the whole line in a file based on keys. Need help!! 1

Status
Not open for further replies.

tulsantide

Technical User
Jun 1, 2015
21
US
Hi,

I need to replace the whole line in file2.txt based on the key in file1.txt
file1.txt
=========
ui_home=/export/appl/website/${ui.root}
java_home=/export/appl/java/ssss
domain.host.list=server-1,server-2
domain.server.cluster.list=server-1:cluster1,server-2:cluster2
runtime=/export/appl/website/runtime
ldap.provider.url=ldap://ldapserver:280
file2.txt
========
ui_home=/export/appl/ui/${test}
java_home=/export/appl/java/zzzzz
domain.host.list=server-10,server-12
domain.server.cluster.list=server-10:cluster1,server-12:cluster2
runtime=/export/appl/website/runtime
ldap.provider.url=ldap://ldapserver:480
ai.home=/export/appl/ddss/shss

Result should be as below
======
file2.txt
=======
ui_home=/export/appl/website/${ui.root}
java_home=/export/appl/java/ssss
domain.host.list=server-1,server-2
domain.server.cluster.list=server-1:cluster1,server-2:cluster2
runtime=/export/appl/website/runtime
ldap.provider.url=ldap://ldapserver:280
ai.home=/export/appl/ddss/shss


Here is the script that I came up, which is giving the following error.Please help me on this.
============================================================
Error:sed: -e expression #1, unknown option to `s'

==========================================================
#!/bin/sh
for i in `cat file1.txt`
do
key=`echo $i|awk -F= '{print $1}'`
value=`echo $i|awk -F= '{print $2}'`
echo "key=$key value=$value"
sed -i "s/${key}.*/$i/" test2.txt
if [ $? -ne 0 ];then

printf "************************************************************************\n"

echo "Token replacement in file2.txt failed"

printf "\n************************************************************************\n"
exit 1

fi
done
 
Hi

If neither the key nor the values can contain equality sign ( = ), it is pretty simple :
Code:
awk -F = -v OFS== 'FNR==NR{k[$1]=$2;next}$1 in k{$2=k[$1]}1' file1.txt file2.txt > new-file2.txt


Feherke.
feherke.ga
 
Wow!!. It worked like a charm. I'm new to shell scripting. I don't understand the above command and it doesn't look simple to me :). If possible can you please explain the same command. Thanks for your help!!
 
Hi

Code:
awk \ 
    -F = \             [gray]# set FS ( Field Separator ) to "=" so automatically splits lines to key ( $1 ) - value ( $2 ) pairs[/gray]
    -v OFS== \         [gray]# set OFS ( Output Field Separator ) to "=" so automatically place it between the fields on output[/gray]
    '
        FNR == NR {    [gray]# FNR ( File Number of Records ) and NR ( Number of Records ) are equal only while processing the 1[sup]st[/sup] input file[/gray]
            k[$1] = $2 [gray]# store the key - value pair in array k[/gray]
            next       [gray]# jump to processing the next input record ( skip the rest of the code -- it is for processing the 2[sup]nd[/sup] input file )[/gray]
        }

        $1 in k {      [gray]# current key has and entry in array k[/gray]
            $2 = k[$1] [gray]# replace the current value with the matching one stored in array k[/gray]
        }

        1              [gray]# always perform default action ( print the current record ) [sup](*)[/sup][/gray]
    ' file1.txt file2.txt > new-file2.txt
[small](*) See Actions in Awk documentaion for more.[/small]


Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top