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

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

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

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

(OP)
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

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

Hi

If neither the key nor the values can contain equality sign ( = ), it is pretty simple :

CODE --> command line

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

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

(OP)
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!!

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

Hi

CODE

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

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

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


Feherke.
feherke.ga

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

(OP)
Thanks!! a lot feherke.

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