Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a simple print function

Status
Not open for further replies.

madasafish

Technical User
Jul 18, 2006
78
TH
I want tbe able to call a nawk function that will print out a set of lines. The first time the function
is called it will print to the screen. The second time it is called I will call the funtion with a ">" at the end
to re-direct output to a file. I appreciate I could repeat all the lines with the ">" but cant help thinking
there must be a better way of doing this in Awk/Nawk
I have no problem dong this in "ksh" but am finding it very difficult to do with awk and nawk.
Is there an Awk/Nawk easy way do this?


#Example ksh program
function printsection
{
echo "This is line 1"
echo "This is line 2"
echo "This is line ...."
}

printsection
more code in here....
.......
.......
printsection > myfile

exit 0

#Awk/Nawk Program
nawk '{

function printawksection(){
printf "\n" "This is line 1"
printf "\n" "This is line 2"
printf "\n" "This is line ...."
}
} END {

printawksection
more code in here....
.......
.......
printawksection > myfile

}'

exit 0
 
code
if ( flag == "d" )
{
printf "%55s\n","LICENSES"
printf "%55s\n","================="
printf "\n"
printf "%-15s %-15s %-15s\n","Common","AA","BB"
printf "%-15s %-15s %-15s\n","========","========","========"
for(j=1;j<=i[1]||j<=i[2]||j<=i[3];++j)
printf "%-15s %-15s %-15s\n",a[3,j],a[1,j],a[2,j]
printf "\n"
printf "%-15s %-15s %-15s %-15s %-15s\n","Common","AA","BB","Total Used","Available"
printf "%-15s %-15s %-15s %-15s %-15s\n","========","========","========","========","========"
printf "%-15s %-15s %-15s %-15s %-15s\n",i[3],i[1],i[2],usedlicense,(maxlicense - usedlicense)
printf "%-15s %-15s %-15s %-15s %-15s\n","========","========","========","========","========"
}
if ( flag == "m" && ( usedlicense > (threshold / 100 * maxlicense) ))
{
printf "\n" > i2mailtxt
printf "%s\n","License Usage" > i2mailtxt
printf "%s\n","=======================" > i2mailtxt
printf "%s\n","Total Purchased: = " maxlicense > i2mailtxt
printf "%s\n","Total Used : = " usedlicense > i2mailtxt
printf "%s\n","Total Available: = " (maxlicense - usedlicense) > i2mailtxt
printf "%s\n","=======================" > i2mailtxt
printf "\n" > i2mailtxt
printf "%s\n","See attachment for details" > i2mailtxt
printf "\n" > i2mailtxt
cmd = sprintf(script" -d > "i2mailatt) color red <----Calls this program with -d flag (only problem is, it resets variables at the beginning of the script. /color
system( cmd )
close ( cmd )
}
/code

From your answer can I deduce that calling a sub-section in an awk progam is not possible - similiar to the ksh example shown above?
 
Hi

Sorry, I understood nothing from your code. But one thing is clear : you can run [tt]awk[/tt] script from [tt]ksh[/tt] and [tt]ksh[/tt] script from [tt]awk[/tt], but that not stands for functions in neither direction. Functions can be called where they are declared.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top