Substitute variable inside nawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substitute variable inside nawk
# 1  
Old 05-27-2014
Substitute variable inside nawk

Hi,

I need to set "prd" in the below command to a unix variable

Code:
nawk '/^#/ {next} FNR==NR {prd[$0];next} !($0 in prd)'

So, this is what i did

Code:
fname=prd   // unix shell variable

nawk -v fname=$fname '/^#/ {next} FNR==NR {fname[$0];next} !($0 in fname)'

But the value of fname i.e "prd" is not getting substituted in the command.

Can you tell me why ?

Code:
OS: SunOS mymac 5.10 Generic_150400-09 sun4v sparc SUNW,SPARC-Enterprise-T5220

# 2  
Old 05-27-2014
Quote:
Originally Posted by mohtashims
Hi,

I need to set "prd" in the below command to a unix variable

Code:
nawk '/^#/ {next} FNR==NR {prd[$0];next} !($0 in prd)'

So, this is what i did

Code:
fname=prd   // unix shell variable

nawk -v fname=$fname '/^#/ {next} FNR==NR {fname[$0];next} !($0 in fname)'

But the value of fname i.e "prd" is not getting substituted in the command.

Can you tell me why ?

Code:
OS: SunOS mymac 5.10 Generic_150400-09 sun4v sparc SUNW,SPARC-Enterprise-T5220

You have 2 variables called 'fname':
  1. Scalar fname passed into awk by: -v fname=$fname
  2. Array fname you're populating with $0: fname[$0]
What's the purpose of passing a scalar fname to awk?
What exactly are you after?
# 3  
Old 05-28-2014
Quote:
Originally Posted by vgersh99
You have 2 variables called 'fname':
  1. Scalar fname passed into awk by: -v fname=$fname
  2. Array fname you're populating with $0: fname[$0]
What's the purpose of passing a scalar fname to awk?
What exactly are you after?
As i explained in the OP all i want is instead of using "prd" in the command i wish to use unix variable fname.

So, the problem is the below is not working !!
  1. Scalar fname passed into awk by: -v fname=$fname
Can you now help ?
# 4  
Old 05-28-2014
Quote:
Originally Posted by mohtashims
As i explained in the OP all i want is instead of using "prd" in the command i wish to use unix variable fname.

So, the problem is the below is not working !!
  1. Scalar fname passed into awk by: -v fname=$fname
Can you now help ?
Maybe this will show you some light

Code:
echo 'Show me the content of $fname' | awk -v fname=$fname '{print fname}'

If you can see prd in your stdout, then that's not the problem.
# 5  
Old 05-28-2014
Hammer & Screwdriver

Let me rephrase my question.

I am using the below code to find the difference between two files prod.properties and back.properties.

Code:
nawk '/^#/ {next} NR==FNR {prod[$0]=$0;next} {if ($0 in prod) { delete prod[$0] } else {print }}; END {for (i in prod) print prod[i]}' prod.properties back.properties

Source: https://www.unix.com/302903183-post5.html

Now, the requirement is to automate the above code so that it does the job for any two file names not neccessarily prod.properties and back.properties

I am passing any two filenames like this.

Code:
./check.sh filename1.properties filename2.txt

and reading them in variables file1=$1, file2=$2.

So, how can I achieve that ?
# 6  
Old 05-28-2014
Assuming that check.sh contains:
Code:
nawk '/^#/ {next} NR==FNR {prod[$0]=$0;next} {if ($0 in prod) { delete prod[$0] } else {print }}; END {for (i in prod) print prod[i]}' prod.properties back.properties

change it to:
Code:
file1="$1"
file2="$2"
nawk '/^#/ {next} NR==FNR {prod[$0]=$0;next} {if ($0 in prod) { delete prod[$0] } else {print }}; END {for (i in prod) print prod[i]}' "$file1" "$file2"

or just:
Code:
nawk '/^#/ {next} NR==FNR {prod[$0]=$0;next} {if ($0 in prod) { delete prod[$0] } else {print }}; END {for (i in prod) print prod[i]}' "$1" "$2"

or even:
Code:
nawk '/^#/ {next} NR==FNR {prod[$0]=$0;next} {if ($0 in prod) { delete prod[$0] } else {print }}; END {for (i in prod) print prod[i]}' "$@"


Last edited by Don Cragun; 05-28-2014 at 06:52 AM.. Reason: Fix typos in file operands add additional option.
# 7  
Old 05-28-2014
So the "prod" inside nawk is just a variable ?

I thought it has some relation with the prod.properties

Please confirm.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to avoid arguments inside Nawk command?

Hi, Here is my command print $2 was meant to select the second column however, it is getting substituted with the second argument that was passed to the script. Can you please tell me how can I resolve this ? (6 Replies)
Discussion started by: mohtashims
6 Replies

2. Shell Programming and Scripting

AWK variable substitute issue

dear, I have below file called folderlist.txt # ParentFolder environment_flag SubFolders triss 1 checksum bookstructure 1 fx 1 checksum_GMDB I have a script which which will create the folders under... (3 Replies)
Discussion started by: manas_ranjan
3 Replies

3. Shell Programming and Scripting

Reading input record from inside nawk

Hi friends, I have small query with reg to awk search pattern.. below is my sample file and code which i tried.. $ cat file.txt xxx,yyyyy,messageID,sha xxxx,errorcode,messageID,name in the above sample file - let assume I know the errorcode(2nd record) using which I want to... (2 Replies)
Discussion started by: Shahul
2 Replies

4. Shell Programming and Scripting

substitute variable in bash

hi all, Assume that i a having the following three lines in an executable file #/bin/bash a=Tue Tue=1 When i give echo $a the value should be 1, how to do this. Your suggestions please. Thanks in advance, Anish (4 Replies)
Discussion started by: anishkumarv
4 Replies

5. Shell Programming and Scripting

substitute variable for values in perl

hi all, how do i assign values passed in from command line to and sql statement in perl ?? e.g i want to assign :name1 and :Name2 to be whatever is passed into the perl script command line my $sqlStr = "select * from test_table where column1 = upper(nvl(:name1, name1 )) and column2... (1 Reply)
Discussion started by: cesarNZ
1 Replies

6. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

7. Shell Programming and Scripting

using cp command inside nawk

Hello I have facing issue while using cp command inside nawk block. #!/bin/ksh my_name=$1 nawk -v my_name1=$my_name 'BEGIN { n = split(my_name1,names,":"); for (i=1;i<=n;i++) { print names; cpcmd = "cp " /tmp/test.txt" " ./sample system(cpcmd) } exit }' exit 0 i'am getting... (1 Reply)
Discussion started by: piscean_n
1 Replies

8. Shell Programming and Scripting

case command inside awk/nawk

well I found lot of topics about awk..about if command in awk.. but I had to implement this: nawk -F"|" ' $47 ~ /0R0011/ { print > ("/home/user/M/MC.tmp" )} $47 ~ /0R0012/ { print > ("/home/user/M/DuSI.tmp" )} $47 ~ /0R0014/ { print > ("/home/user/M/FF.tmp" )} $47 ~ /0R0018/ { print >... (9 Replies)
Discussion started by: abdulaziz
9 Replies

9. UNIX for Dummies Questions & Answers

Substitute Variable

Can anyone tell me what is the purpose of a substitute variable in the unix programming language and give an example where it may be used? Thanks! (0 Replies)
Discussion started by: mmg2711
0 Replies

10. Shell Programming and Scripting

Substitute variable values

Hi, I am trying to redefine the value of a variable based on another variable value. And I want to read in my variables from a enviroment file in the end -- at least I think so. But 1st here's what I want I need to get working: var_1="11 10 9 8 7 6 5 4 3 2 1" var_2=3 var_3=4 So I want... (12 Replies)
Discussion started by: John Rihn
12 Replies
Login or Register to Ask a Question