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.
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.603kudos 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
– pabouk – 2013-11-15T12:02:49.377#!
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.@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 stringsabab
andbcbc
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.163need 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