Help nawk change external variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help nawk change external variable
# 1  
Old 07-14-2011
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

Code:
rownumber=0
for FILE in $FILELIST
do
    filename=`basename $FILE`
    curvename=`echo $filename | awk '{FS="."}{print $1}'`
    rundate=`echo $filename | awk '{FS="."}{print $2}'`
    echo $curvename
    nawk '
    FNR==1 {
        numheadrecords = $1;
        rowstoprocess  = numheadrecords + 2;
        next;
    }
    FNR<rowstoprocess {
        RCount = RCount + 1;
        julianmdate = $1;
        rate        = $3;
        mdate      = $4
        printf "%s,%s,%s,%s,%s,%s\n", LowFile, RDate, mdate, julianmdate, rate, RCount;
    }
    ' LowFile=$curvename RDate=$rundate RCount=$rownumber $FILE >>$OUTPUTFILE
done


Last edited by srattani; 07-14-2011 at 03:06 PM..
# 2  
Old 07-14-2011
awk doesn't have access to shell variables, you'll have to store it somewhere.

Code:
 awk '

...

END {        print RCount > "/dev/stderr" }' filename >> outname 2> varfile
VAR=`cat varfile`

You could also just do everything inside awk, and wouldn't need to worry about getting variables into and out of it.
# 3  
Old 07-14-2011
Hi,

Thanks for your reply. In my case If you see I do not have an END, so when would I know that nwak is done processing the rows and I should store RCount in a file.

I am assuming initially I will have to store RCount as 0 in varfile. Inside nwak I have to read it from file keep incrementing and thne once done I write it out to the file?

Please advise how should I accomplish it, I am new to Unix scripting so I would really appreciate your help.
# 4  
Old 07-14-2011
Quote:
Originally Posted by srattani
Hi,

Thanks for your reply. In my case If you see I do not have an END, so when would I know that nwak is done processing the rows and I should store RCount in a file.
That's okay, just add the END { } block after the other blocks. It should start on its own line, just like all the other blocks you have in that awk statement. END is a special block that gets run when awk runs out of input.
Quote:
I am assuming initially I will have to store RCount as 0 in varfile.
Nope, you don't. Just do what I showed you.
Quote:
Inside nwak I have to read it from file keep incrementing and thne once done I write it out to the file?
Nope, you don't, just do what I showed you.

Sequence of events is:
  1. Shell variable copied into awk
  2. awk finishes processing data, calls END {} block, prints new value into stderr, which is saved to varfile.
  3. awk quits.
  4. shell reads varfile back into the variable.

Awk and the shell are separate so awk can't affect each others' variables. So awk just prints the variable into a file when its done for the shell to read and update itself with.

Last edited by Corona688; 07-14-2011 at 04:18 PM..
# 5  
Old 07-14-2011
Hi,

I still dont exactly follow. Will it be something like follows?

Code:
nawk '
    FNR==1 {
        numheadrecords = $1;
        rowstoprocess  = numheadrecords + 2;
        next;
    }
    FNR<rowstoprocess {
        RCount = RCount + 1;
        julianmdate = $1;
        rate        = $3;
        mdate      = $4
        printf "%s,%s,%s,%s,%s,%s\n", LowFile, RDate, mdate, julianmdate, rate, RCount;
    }
    END { print RCount > "/dev/stderr" }' filename >> outname 2> varfile
VAR=`cat varfile`}
    ' LowFile=$curvename RDate=$rundate RCount=$rownumber $FILE >>$OUTPUTFILE

# 6  
Old 07-14-2011
You're taking my example way too literally. "filename >> outname 2> varfile" was outside the awk statement I showed you and certainly shouldn't be inside yours. (The > "/dev/null" belongs inside, though.)

The VAR=`cat varfile` is not part of the awk statement, it happens after. It reads varfile into VAR inside the shell, getting back the value awk saved when it quit. I don't know how you ended up with the } after it. Again it's not meant to be taken literally -- you wanted rowcount, not VAR.

Come to think of it a faster way to do it is read rownumber <varfile

So:

Code:
rownumber=0
for FILE in $FILELIST
do
    filename=`basename $FILE`
    curvename=`echo $filename | awk '{FS="."}{print $1}'`
    rundate=`echo $filename | awk '{FS="."}{print $2}'`
    echo $curvename
    nawk '
    FNR==1 {
        numheadrecords = $1;
        rowstoprocess  = numheadrecords + 2;
        next;
    }
    FNR<rowstoprocess {
        RCount = RCount + 1;
        julianmdate = $1;
        rate        = $3;
        mdate      = $4
        printf "%s,%s,%s,%s,%s,%s\n", LowFile, RDate, mdate, julianmdate, rate, RCount;
    }
    END { print RCount > "/dev/stderr" }
    ' LowFile=$curvename RDate=$rundate RCount=$rownumber $FILE >>$OUTPUTFILE 2> varfile
    read rowcount <varfile
done

# delete temporary file when you're done
rm -f varfile

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 07-14-2011
Thank you very much, worked like a charm. Appreciate all your patience and help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Date format conversion how to change this from using nawk to awk

Hi, I have a file where I need to change the date format on the nth field from DD-MM-YYYY to YYYY-MM-DD so I can accurately sort the record by dates From regex - Use sed or awk to fix date format - Stack Overflow, I found an example using nawk. Test run as below: $: cat xyz.txt A ... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. 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

3. 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

4. Shell Programming and Scripting

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 while read groupsvar do ... (5 Replies)
Discussion started by: Jazmania
5 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. 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

9. Shell Programming and Scripting

is it possible to pass external variable values to nawk?

Dear friends, please tell me how to pass the external variable values to the nawk command. length=`expr $len2 - $len1` i need to pass $length to following nawk command as mentioned below. nawk '{if((x=index($0,"W/X"))>0){id=substr($0,x, $length);print x;print id;}}' filename1 but I am... (1 Reply)
Discussion started by: swamymns
1 Replies

10. UNIX for Dummies Questions & Answers

Stupid question change privilges on an external volume?

Hi I'm using bsch? on an OSX machine, how can I change privilges on an external volume? It is read only and I want it r+w. I can navigate to it, see it read it, but chmod won't do what I want. (1 Reply)
Discussion started by: andbro
1 Replies
Login or Register to Ask a Question