Access Awk Variables Outside Scope


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Access Awk Variables Outside Scope
# 1  
Old 10-26-2006
Access Awk Variables Outside Scope

My awk script searches for specified patterns in a text file and stores these values into mem variables.
Once this is done I want to Insert these values into a table.

How can I avail of the variable values outside the scope of awk script....

One method that I have tried is to write the variables values into a temp file with delimiters and then read these values from a shell script, form a connection to database and insert the values into table....But I feel this is doing things in a roundabout way....

What wud be a straighter approach ? I dont want to sacrifice the awk script !

The awk script now :

Code:
/Total logical records skipped:/ {skiprecs=$5;}
/Total logical records read:/ {readrecs=$5;}
/Total logical records rejected:/ {rejectrecs=$5;}
/Total logical records discarded:/ {discardrecs=$5}
END { totalfailrecs = skiprecs + rejectrecs + discardrecs ;
      totalsuccessrecs = readrecs ;
      totalrecs = totalfailrecs + totalsuccessrecs ;
      printf "Printing the results "
      printf ("\n")
      printf ("Failed Records : %d\n", totalfailrecs);
      printf ("Success Records : %d\n", totalsuccessrecs);
      printf ("Total Records : %d\n", totalrecs);
      printf ("%d,%d,%d",totalfailrecs,totalsuccessrecs,totalrecs)>>"tempfile"
    }

Thanks
Amruta Pitkar
# 2  
Old 10-26-2006
I don't think we can retrieve values from awk to unix shell... when awk finishes we loose all it values... I can provide you another alternative method instead of creating and parsing it..

Code:
a1.awk

/Total logical records skipped:/ {skiprecs=$5;}
/Total logical records read:/ {readrecs=$5;}
/Total logical records rejected:/ {rejectrecs=$5;}
/Total logical records discarded:/ {discardrecs=$5}
END { totalfailrecs = skiprecs + rejectrecs + discardrecs ;
      totalsuccessrecs = readrecs ;
      totalrecs = totalfailrecs + totalsuccessrecs ;
      printf ("%d,%d",totalfailrecs,totalsuccessrecs);  
    }


Code:
#/usr/bin/ksh

oput=$(awk -f a1.awk inputfile)
totalfailrecs=cut -d"," -f1
totalsuccessrecs=cut -d"," -f2

total_recs=$(($totalfailrecs+$totalsuccessrecs))

do what ever you want after this

# 3  
Old 10-26-2006
This is clunky but will work:
Add this one last line in the END section:
Code:
printf ("FAIL=%d\nSUCCESS=%d\nTOTAL=%d",totalfailrecs,totalsuccessrecs,totalrecs)>"temp.sh"

in your shell script
Code:
chmod +x ./temp.sh
. ./temp.sh

This will "import" three variables: FAIL SUCCESS TOTAL
# 4  
Old 10-26-2006
Great... I got an enhancement to it

Code:
printf ("FAIL=%d;SUCCESS=%d;TOTAL=%d",totalfailrecs,totalsuccessrecs,totalrecs);


then call the awk script in shell script

Code:
str=$(awk -f test.awk inputfile)

eval $str

echo $FAIL
echo $SUCCESS
echo $TOTAL

# 5  
Old 10-26-2006
Code:
eval `echo $* | awk -F: '{printf("a=%s; b=%s; c=%s\n", $1, $2, $3)}'`
print $a
print $b
print $c

I got this tip somewhere in this forum.
# 6  
Old 10-26-2006
Anbu23

The above mentioned stuff does the samething as what you posted... seems like you have searched whole forum and posted the same Smilie
# 7  
Old 10-26-2006
Quote:
Originally Posted by mahendramahendr
Anbu23

The above mentioned stuff does the samething as what you posted... seems like you have searched whole forum and posted the same Smilie
I got this tip when i was going through the archive two months back. But now i don't remember who posted it or the link.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Regarding the protected variables access

Hello forum, I am siva working as programmer .I was blocked with the below issue so please help any of the forum memebers. testve.h class cv { protected : struct state; state& m_state; }; testVe.cpp struct state { m_size; } the above are 2 files which have the... (3 Replies)
Discussion started by: workforsiva
3 Replies

2. Shell Programming and Scripting

Scope of variables between scripts

Friends, I am using ksh under SunoS. This is what I have In file1.sh NOW=$(date +"%b-%d-%y") LOGFILE="./log-$NOW.log" I will be using this file through file1.sh as log file. I have another script file2.sh which is being called inside my file1.sh. I would like to use the same log... (6 Replies)
Discussion started by: dahlia84
6 Replies

3. Shell Programming and Scripting

while read loop; scope of variables (shell)

If I set a variable within a while-read loop, sometimes it's local to the loop, sometimes it's global, depending on how the loop is set up. I'm testing this on a Debian Lenny system using both bash and dash with the same results. For example: # Pipe command into while-read loop count= ls -1... (2 Replies)
Discussion started by: mjd_tech
2 Replies

4. Shell Programming and Scripting

Variables scope.

Hi , I'm trying to change the variable value in a while loop , however its not working it seems that the problem with subshells while reading the file. #!/bin/sh FLAG=0; cat filename | while read data do FLAG=1; done echo $FLAG Should display 1 instead displays 0 (13 Replies)
Discussion started by: dinjo_jo
13 Replies

5. Shell Programming and Scripting

Doubt about variables scope

I call my script with two parameters myscript.sh aaa bbb What is the way to access $1 and $2 values inside a function? I call the function like this myfuntion $1 $1 but inside of the function, $1 and $2 are empty. Any suggestions? thank you in advanced. (1 Reply)
Discussion started by: aristegui
1 Replies

6. Shell Programming and Scripting

Access variables from profile file

Hi all, I hav created one profile file in which i have declared some variables i want to knw how can i access these variables into my xyz.ksh file when i am trying to do " echo $variablename " its giving me an empty line i have done export variable also in the profile file. Thanks in... (6 Replies)
Discussion started by: navi
6 Replies

7. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

8. Shell Programming and Scripting

Doubt??? [scope of variables]

Heres an example..... <~/abc>$ cat textfile line 1 line 2 line 3 line 4 line 5 <~/abc>$ cat try.sh #/bin/ksh for runs in 1 2 3 do A=$runs echo "Inside A : $A" done echo "Outside A : $A" <- works fine (1 Reply)
Discussion started by: qzv2jm
1 Replies

9. Shell Programming and Scripting

How to access variables across scripts

Hi All, I have declared a variable in script1 and assign a value for it. In script2 i'll call script1 and then I want the value of variables set in script1. I have tried with export, but in vain. How can I achive this? Below is the two scripts. --script1 #!/usr/bin/ksh echo $1... (1 Reply)
Discussion started by: javaDev
1 Replies

10. UNIX for Advanced & Expert Users

Access Awk Variables Outside Scope

Sorry in the wrong forum. Moving this to right forum. (2 Replies)
Discussion started by: Amruta Pitkar
2 Replies
Login or Register to Ask a Question