Nawk variable question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nawk variable question
# 1  
Old 06-27-2012
Nawk variable question

Hi,
I'm trying to replace a string with the contents of a file.. I found the below thread that shows how to do this but is there any way to use a variable as the file name in the nawk command?.. When I try I get an error saying
Quote:
cat: cannot open _temp
Code:
        while read groupsvar
        do      
                replacevar="%${groupsvar}"

                nawk -F"$replacevar" 'NF>1{printf "%s",$1;\
                for (i=2;i<=NF;i++){system("cat ${groupsvar}_temp");printf "%s",$i ((i==NF)?"\n":z)}}NF==1' 
${entries} > ${entries}_1001
        done < ${grouplist}_temp2

https://www.unix.com/shell-programmin...strings-2.html

Last edited by vbe; 06-27-2012 at 11:25 AM..
# 2  
Old 06-27-2012
Code:
"cat ${groupsvar}_temp"

...
cat is giving the error message, meaning ${groupsvar} is null or not initialized correctly... and so cat is trying to open _temp that does not exist...
# 3  
Old 06-27-2012
Quote:
Originally Posted by vbe
Code:
"cat ${groupsvar}_temp"

...
cat is giving the error message, meaning ${groupsvar} is null or not initialized correctly... and so cat is trying to open _temp that does not exist...
But if I echo out ${groupsvar} after the do it is returning the correct data..
# 4  
Old 06-27-2012
The AWK script is a single-quoted string. There is no shell parameter expansion occurring within it, so the variable isn't replaced in the script before AWK is executed. Further, the shell spawned by the system command has no notion of that variable as it has no access to it via the command line nor its environment.

If you instead provide a sample of the data in those files along with the desired result and what you're trying to accomplish, we can probably guide you towards a solution that is less convoluted.

Regards,
Alister
# 5  
Old 06-27-2012
What OS and shell are you using?
# 6  
Old 06-27-2012
Quote:
Originally Posted by vbe
What OS and shell are you using?
Using ksh and its solaris.. In the end i just did the below and it got around it..

Code:
groupsvar1=$SUDOREP_WORK/$groupsvar; export groupsvar1


However its not what I hoped it would be...


What I'm trying to do is replace the group names (which are identified with a % sign in front of them) in the last field of the AIX_vintella.sudoers_entries file attached with their actual user id and name...

I've managed to dump the user id's and names into a separate file for each group.. Like the webstaff_temp file attached..

I was using a list file (AIX_vintella.sudoers_grouplist_temp2) to try and cycle through each group and replace the match in the *entries file..

But alas it doesn't work properly and I don't know what direction to go any more with this... Smilie

Last edited by Jazmania; 06-27-2012 at 12:57 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substitute variable inside nawk

Hi, I need to set "prd" in the below command to a unix variable nawk '/^#/ {next} FNR==NR {prd;next} !($0 in prd)' So, this is what i did fname=prd // unix shell variable nawk -v fname=$fname '/^#/ {next} FNR==NR {fname;next} !($0 in fname)'But the value of fname i.e "prd" is not... (8 Replies)
Discussion started by: mohtashims
8 Replies

2. Shell Programming and Scripting

issue with nawk while using variable assignment

#ifconfig -a | nawk '/1.1.1.1/{print}' inet 1.1.1.1 netmask xxxxxxxxx broadcast 0.0.0.0 If i assign the ip to a variable and search for the variable nothing gets printed!! # ifconfig -a | nawk -v ip=1.1.1.1 '/ip/{print}' I am not able to understand why this is happening! (6 Replies)
Discussion started by: chidori
6 Replies

3. Shell Programming and Scripting

Help nawk change external variable

Hello, I have external variable rownumber, I am processing files within a loop and I would like to keep incrementing rownumber. What is happening is inside nawk section it passes rownumber but it never gets updated. I want to update rownumber inside the nawk, I would appreciate your help ... (6 Replies)
Discussion started by: srattani
6 Replies

4. Shell Programming and Scripting

nawk question

Hi guys I am using the following command to "grep" a pattern and 22 lines after it from the file: $ nawk '/\/{c=22}c&&c--' allocations.ini It does the job fine if the pattern is hardcoded into the command, if the case above it is . But when I am trying to assign a variable instead of... (7 Replies)
Discussion started by: aoussenko
7 Replies

5. Shell Programming and Scripting

Using variable in NAWK

I'm trying the following script using nawk and failed to call variable inside the script. echo "Enter the absolute path of XML location for respective billcycle" read xml_location grep "string to find:" abcd.log|cut -d '/' -f12|nawk '{print $xml_location $1}' > redirected_log.log Please... (2 Replies)
Discussion started by: nadvi
2 Replies

6. Solaris

NAWK - setting a variable to the highest value

Hi, new to nawk so not sure how this works, I have a file with three values 23:36:18 - i need to set a variable called SCORE with the highest value from the file (i.e. 36) using nawk can anyone help? thanks (1 Reply)
Discussion started by: Pablo_beezo
1 Replies

7. Shell Programming and Scripting

Passing shell variable to NAWK

I am trying to make a simple script in which i take input from shell and then forward the value to nawk (BEGIN). but when i run below mention script so it give no output. echo "Enter TRUNK GROUP:" read TGR cat /omp-data/logs/5etr/080422.APX | nawk -F"|" -v P=$TGR ' BEGIN { TG=P;... (1 Reply)
Discussion started by: wakhan
1 Replies

8. Shell Programming and Scripting

nawk question

I am trying to write the following c code in nawk: while ((ch = getc (fp)) != EOF) { total_bytes++; checksum = (checksum >> 1) + ((checksum & 1) << 15); checksum += ch; checksum &= 0xffff; /* Keep it within bounds. */ } I appreciate your help (10 Replies)
Discussion started by: DeltaX
10 Replies

9. Shell Programming and Scripting

NAWK question

Hello everybody !! I just started messing around with NAWK/AWK and I find it very interesting. I was trying to have script that prints only the the different lines (lines that are identical and adjacent, some that are identical and not adjacent, and some that are just different) I tried... (6 Replies)
Discussion started by: DeltaX
6 Replies

10. UNIX for Dummies Questions & Answers

Passing a for loop variable into nawk

After searching through books and the internet for days I can't seem to find an example of this. I'm trying to pass a variable from a for loop into nawk but can't seem to get all the syntax right. my script (thanks to anbu23 for nawk help) is this: for customers in `cat customers.txt` do... (3 Replies)
Discussion started by: Ant1815
3 Replies
Login or Register to Ask a Question