match patterns update output file uncomment when desired

0

1

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.

user2692634

Posted 2013-11-13T13:15:40.027

Reputation: 29

Answers

0

Here is a shell + sed script which will pick the variable names and values (in front of the loop) and then edit them into the file (in multiple passes inside the loop):

#!/bin/sh

sed -nr 's/^([^=#][^=]*)=(.*)$/\1\n\2/p' "$1" |
    while read var ; read val ; do
        sed -ri "s/^#?($var=) *$/\1$val/" "$2"
    done

First parameter of the script is the file with values (myfile.txt). Second is the file to fill (responsefile.txt). The second file is edited in place. The script does not treat white space specially (except spaces after = in responsefile.txt) as it was not clear in the question how it should be.

Here is extension of your AWK script completed to be functional in all cases presented in your demo files. First it will fill the associative array from the first file then it will modify the content of the second file and send it to stdout. I am not presenting it as a one-liner to keep it readable:

#!/usr/bin/awk -f

BEGIN   { FS="=" }
NR==FNR { a[$1]=$0; next }
$1 in a { $0=a[$1] }
/^#/    { var=$1; sub(/^#/, "", var); if(var in a) { $0=a[var] } }
1

Your script just needed a special treatment for commented assignments. The parameters of the script are the same as in the first script but the output is sent to stdout and the second file is left unmodified.

pabouk

Posted 2013-11-13T13:15:40.027

Reputation: 5 358

Thanks for your efforts. Actually my idea is to fill a product response file=responsefile.txt using input file=myfile.txt, as per my above example. – user2692634 – 2013-11-14T10:16:35.193

@user2692634: That is what the script does. You have to call it: with the files given as parameters: ./scriptname myfile.txt responsefile.txt. The script will modify the second file - responsefile.txt. Because the script does not use associative arrays (used in your AWK code) it uses the second file to store the intermediate results between multiple edit passes. I will probably also add an AWK variant which can go in a single pass. – pabouk – 2013-11-14T10:55:34.603

kudos to you... have a query, will this make any difference #!/bin/sh and #!/usr/bin/awk -f? – user2692634 – 2013-11-15T11:07:01.993

@user2692634: The sequence starting by #! in the first line of the script is so-called shebang. It says which interpreter will be used to run the script. So in the case of /bin/sh it is a Bourne-compatible shell in the case /usr/bin/awk -f it is AWK. You must use the correct shebang according to the language the script is written in.

– pabouk – 2013-11-15T12:02:49.377

@user2692634: I am not sure what do you want to do. What is the exact input (newlines are not shown in comments) and the error messages. --- sed: I do not think that you can separate an unfinished command or block into multiple -e parts. --- AWK: The strings abab and bcbc are part of the script? What is their role? --- Please open a new question. Describe there what do you want to achieve, what did you do and what are the results. Here in a comment you can point me to the question. – pabouk – 2013-11-18T08:03:06.163

need ur help i am trying use combine multiple commands which includes ur script. I am facing following issue. I had put all the commands in one file called WASfile then execute WASfile. Getting errors. Say sed -i -e '/abab/,/bcbc/{w scriptname' -e 'd}' WASfile abab #!/usr/bin/awk -f BEGIN { FS="=" } NR==FNR { a[$1]=$0; next } $1 in a { $0=a[$1] } /^#/ { var=$1; sub(/^#/, "", var); if(var in a) { $0=a[var] } } 1 bcbc. So trying to run as continuous script combined with many scripts or commands. Please suggest. – user2692634 – 2013-11-18T08:34:09.030

@user2692634: Please open a new question and explain all the details as I wrote above. – pabouk – 2013-11-18T08:37:01.017

here is the link for new question link

– user2692634 – 2013-11-18T09:05:41.117