match patterns update output file uncomment when desired

Posted by user2692634 on Super User See other posts from Super User or by user2692634
Published on 2013-11-13T13:15:40Z Indexed on 2013/11/13 16:01 UTC
Read the original article Hit count: 159

Filed under:

Need suggestion for following. Have two files myfile and responsefile.

First file

myfile.txt  

user=myname   
user_1=yourname   
group=mygroup   
group_1=yourgroup

second file

responsefile.txt

#Please fill details  
user=  
#user_1=  
#user_2=  

#Please fill details  
group=  
#group_1=  
#group_2=  

Based on myfile.txt data update responsefile.txt as below and the file responsefile.txt is lenghty of about 604L, 16481C.

Result output

responsefile.txt

#Please fill details  
user=myname  
user_1=yourname  
#user_2=  

#Please fill details  
group=mygroup  
group_1=yourgroup  
#group_2=  

If you observe myfile above, I want to match user= in responsefile, then update as user=myname, same applies for group=. Then match user_1= and group_1= which is hashed or commented in responsefile, update as user_1=yourname and group_1=yourgroup. Should not remove hash or uncomment for others in file.

I tried this

awk -F= 'NR==FNR{a[$1]=$0;next}$1 in a{$0=a[$1]}1' myfile.txt responsefile.txt

Please suggest thanks in advance.

© Super User or respective owner

Related posts about awk