awk substitution of values from onefile to another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk substitution of values from onefile to another
# 1  
Old 02-12-2012
awk substitution of values from onefile to another

Hello Friends,

I am new to unix, trying to use awk/sed utility to substitute all matching variable values from config file to property file , but failing to do so , could you please help me here with your suggestions or command..
Code:
test.cfg file:-
dbpasswd=pswd
path=/opt/apps/oracle
sid=devdb
databaseuser=abc

test.property file:-
Code:
#database
databaseuser=abc
dbpasswd=pswd
path=/opt/apps/oracle
sid=devdb
tns=@@sid@@ ---> I  do not want to replace "sid" within @@
dbenvpswd=@@dbpasswd@@ --> I  don't want to replace dbpasswd value within @@

I tried :
Code:
awk 'BEGIN {OFS=FS="="} {if ($1==cat test.cfg | grep -v "#" |cut -d "=" -f1) sub($2, "cat test.cfg | grep -v "#" |cut -d "=" -f2 ); print} test.property>

final.property

Not sure if am allowed to assign cat vale of file to $1 and substitute it , or missing any thing here.. as it is throwing error
"Illegel statement and syntax error " ..
Please help.

Thanks in advance..
PD

Last edited by Franklin52; 02-13-2012 at 09:46 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 02-12-2012
Hi, you are not allowed to assign value like that, also that is not awk syntax.

This code reads the cfg file first (the NR==FNR bit) and stores the parameters and values into an array.
Then it reads the property file and replaces $2 if $1 exists in the array....
Code:
awk -F= 'NR==FNR{A[$1]=$2;next} $1 in A{$2=A[$1]}1' OFS='\=' test.cfg test.property

# 3  
Old 02-13-2012
Hi Scrutinizer, I appreciate your reply and time on this. I tried your command and found below error

Code:
awk -F= 'NR==FNR{A[$1]=$2;next} $1 in A{$2=A[$1]}1' OFS='\=' test.cfg test.property
awk: syntax error near line 1
awk: bailing out near line 1

So tried below comand, but doesn't seem value from property file is being replaced by config values.
Code:
/usr/xpg4/bin/awk -F= 'NR==FNR{A[$1]=$2;next} $1 in A{$2=A[$1]}1' OFS='\=' test.cfg test.property..

I have different values in config fiels as:
test.cfg file:-
Code:
dbpasswd=abc
# db path
path=/opt/apps/pdr/xyz/
sid=DEVDB2
databaseuser=efg

Please suggest.

Thanks .PD

Moderator's Comments:
Mod Comment Use code tags, see PM.

Last edited by zaxxon; 02-13-2012 at 02:39 PM.. Reason: code tags
# 4  
Old 02-13-2012
Hi PD2, I get:

Code:
$ cat infile1
dbpasswd=abc
# db path
path=/opt/apps/pdr/xyz/
sid=DEVDB2
databaseuser=efg

$ cat infile2
#database
databaseuser=abc
dbpasswd=pswd
path=/opt/apps/oracle
sid=devdb
tns=@@sid@@ ---> I  do not want to replace "sid" within @@
dbenvpswd=@@dbpasswd@@ --> I  don't want to replace dbpasswd value within @@

Code:
$ awk -F= 'NR==FNR{A[$1]=$2;next}$1 in A{$2=A[$1]}1' OFS='\=' infile1 infile2
#database
databaseuser=efg
dbpasswd=abc
path=/opt/apps/pdr/xyz/
sid=DEVDB2
tns=@@sid@@ ---> I  do not want to replace "sid" within @@
dbenvpswd=@@dbpasswd@@ --> I  don't want to replace dbpasswd value within @@

What do you get?
# 5  
Old 02-13-2012
Unfortunately not getting what you got .. -Smilie
Code:
$>/tmp# awk -F= 'NR==FNR{A[$1]=$2;next}$1 in A{$2=A[$1]}1' OFS='\=' infile1 infile2
awk: syntax error near line 1
awk: bailing out near line 1

So tried ;............
Code:
$>:/tmp# /usr/xpg4/bin/awk -F= 'NR==FNR{A[$1]=$2;next}$1 in A{$2=A[$1]}1' OFS='\=' infile1 infile2

Output on command line is :
Code:
\=
#database
databaseuser\=efg
dbpasswd\=abc
path\=/opt/apps/pdr/xyz/
sid\=DEVDB2
tns=@@sid@@ ---> I  do not want to replace "sid" within @@
dbenvpswd=@@dbpasswd@@ --> I  don't want to replace dbpasswd value within @@
\=

When I checked infile2 .. I don't see change in value s Smilie

Not sure what am I missing.

---------- Post updated at 06:09 PM ---------- Previous update was at 05:09 PM ----------

Hey scrutinizer,

your logic is absolutely correct. Only thing is , on my server - sunOS - I need to use nawk , to get corect result.

Thanks you very much , appreciate your quick n right hit ...

Cheers,
PD2

Last edited by Franklin52; 02-14-2012 at 05:30 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 02-14-2012
OK good, I think that /usr/xpg4/bin/awk is actually correct (and is the best awk to use on Solaris) and the = in OFS should not be escaped, but some non-compliant awks may need it.. I think in /usr/xpg4/bin/awk this should work
Code:
awk -F= 'NR==FNR{A[$1]=$2;next}$1 in A{$2=A[$1]}1' OFS== infile1 infile2

# 7  
Old 02-14-2012
You are awesome, Scrutinizer! It (OFS== ) did work with /usr/xpg4/bin/awk on my sunOS server. I have small doubt - why 1' and OFS in A{$2=A[$1]}1' OFS== is needed. What is their significance?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Does awk have parameter substitution?

Can I specify a default value to a variable in AWK like BASH in one statement using parameter substitution? BASH example: argument=${$1-"default if empty"} (BASH) I know I can do: argument=$1; sub ( "^$", "default if empty", argument) (AWK) Mike (13 Replies)
Discussion started by: Michael Stora
13 Replies

2. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

3. Shell Programming and Scripting

awk substitution

Hi all, I need some help with substitution in awk. Is it possible to substitute field from awk output with string from file? For example: zcat /SMS/CDR/cdr_TC/callLogs*_*_2013092710*.gz | sed 's/:/;/g' | awk -F";" '{if($2==1 && $10~/389123456789/) print $36";"$37}' 2;19733248 I want... (6 Replies)
Discussion started by: vasil
6 Replies

4. Shell Programming and Scripting

awk comparison and substitution

Hi, here's my - not so easy to describe - problem: I want to compare the values of one file (FileA) with a cutoff-value and, if this comparison is true, substitute those values with those in the second file (FileB). However, there are many FileA's (FileA), whereas there is only one FileB. Every... (10 Replies)
Discussion started by: waddle
10 Replies

5. Shell Programming and Scripting

AWK: read values from file1; search for values in file2

I have read another post about this issue and am wondering how to adapt it to my own, much simpler, issue. I have a file of user IDs like so: 333333 321321 546465 ...etc I need to take each number and use it to print records wherein the 5th field matches the user ID pulled from the... (2 Replies)
Discussion started by: Bubnoff
2 Replies

6. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

7. Shell Programming and Scripting

Substitution in AWK

I am trying to use AWK to replace dallinux02 to dallinux03 everywhere in the servers.txt file and move it over to "awk2". Here is my script "awk2.awk": gsub(/dallinux02/, "dallinux03"); print > "awk2" I am trying to run this using the following: $ awk -f awk2.awk... (3 Replies)
Discussion started by: ora_umair
3 Replies

8. Shell Programming and Scripting

help with awk substitution

Hi again. A have a CSV-file in the following format: 2008.09.01,15:17:42,9227096485,9233175320,CTC10,SMS,0901151742098314,Target_MSIS DN_is_blacklisted I want to have an awk command that will say: If the first 3 digits of $4 does not begin with 922 or 923, then make $8 say "Invalid... (3 Replies)
Discussion started by: daytripper1021
3 Replies

9. Shell Programming and Scripting

AWK substitution

I need to copy field 2 to field 3 for only those records that have the 1st field equal to account e.g. file account|123|789|xxx|yyy|zzz|... account_group|444|555|xxx|yy|zz|.... account|456|901|aaa|bbb|ccc|..... after running awk script should look like account|123|123|xxx|yyy|zzz|...... (4 Replies)
Discussion started by: klut
4 Replies

10. UNIX for Dummies Questions & Answers

awk variable substitution

for the command below, it looks for the 3rd field value matching "P" and printing it. awk '{if ($3 == "P") print}' file how would i express this if i use a loop to find more that 1 variable fro a list? this doesn't seem to work... cat list | while read n do awk '{if ($3 == "$n") print}'... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question