Sakthisiga
Sakthisiga

Reputation: 93

Shell Script to read a line in a file and replace with value in property file

I have a file names.txt and it has multiple lines of names, like below:

tom
sam
harry
sarrah

and I have a property file which has equivalent value for few names, like below:

tom=tommy
sam=samantha

I have to read each line of file names.txt and if sam or tom found, It has to replace with value from property file and my end result should be like below:

tommy
samantha
harry
sarrah

Please help me in identifying the script for the same

Upvotes: 0

Views: 287

Answers (2)

XING
XING

Reputation: 9886

Note sure if you got your answer or not but I am posting one script which will do your task. The inputs to the script are namefile and propertyfil.

filenm=$1       --Name file as input
lkfilenm=$2     --Property file for lookup

while read line
do

echo $line

z=`grep "$line" /home/$lkfilenm | cut -d"=" -f2`

echo $z

if [ -z "$z" ]; then       
    echo ok
else       
    sed -e "s/$line/$z/g" /home/$filenm > filenme
fi

done<$filenm

mv filenme $filenm
rm -f filenme

Execution: Fire the below command to execute the shell:

ksh script.ksh namefile prptyfile

Upvotes: 0

William Pursell
William Pursell

Reputation: 212424

Not super robust, but you could do:

awk -F= 'NR==FNR{a[$1]=$2; next} 
    { print $0 in a ? a[$0] : $0}' properties names.txt 

Upvotes: 1

Related Questions