awk printf for user defined variables

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk printf for user defined variables
# 1  
Old 10-02-2007
awk printf for user defined variables

I am working on a SunFire 480 - uname -a gives:

SunOS bsmdb02 5.9 Generic_112233-08 sun4u sparc SUNW,Sun-Fire-480R

I am tyring to sum up the total size of all the directories for each running database using awk:


for Database in `ps -efl | grep "ora_pmon" | grep -v grep | awk '{printf("%s\n"
,$NF)}' | cut -f3 -d'_' | sort`

export Database
# echo "$Database\c"

find / -type d -name $Database -exec du -sk {} \; 2>/dev/null |
awk '{
sum += $1
printf("%10s\t%20s\n",$Database,sum) }'



I can get output in the format Database NNNNNNNN using the echo command, now commented out. If I try to use printf in the awk statement to format the output for both Database name and sum, the printf statement does not pick up the value of $Database (or Database for that matter). If I use the echo command for the Database name, the output format is difficult to control. How can I get the database name recognised in the printf statement?


# 2  
Old 10-02-2007
One way:
find / -type d -name $Database -exec du -sk {} \; 2>/dev/null |
awk -v Db=$Database '{
sum += $1
printf("%10s\t%20s\n",Db,sum) }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Call user defined function from awk

My requirement is to call function ("fun1") from awk, and print its returned value along with $0. fun1() { t=$1 printf "%02d\n", $t % 60; } echo "Hi There 23" | awk '{print $0; system(fun1 $3)}' Any suggestions what to be modified in above code to achieve requirement.. (5 Replies)
Discussion started by: JSKOBS
5 Replies

2. UNIX for Beginners Questions & Answers

How are environment variables defined in a Gnome terminal session?

Hello... and thanks in advance for any help anyone can offer me with my question! I'm hoping someone will have a little patience with me and walk me through this! I'm trying to understand a user login process on Centos 7 and I've gotten a bit confused trying to figure out when/how a Gnome... (4 Replies)
Discussion started by: bodisha
4 Replies

3. Shell Programming and Scripting

Doing math using user defined input and system environment variables

Hi, I need some help to setup some environmental variables. for example...Get A -> userdefined/user input B -> a number. c -> system variable...for example $GETCONF PAGE_SIZE E = do some math using bc display a message "The value is E" setup the system/kernel paramter sysctl -p... (3 Replies)
Discussion started by: saravanapandi
3 Replies

4. Shell Programming and Scripting

Question about awk - create a user-defined variable

Hi, guys, The content of file is below (from <UNIX® Shells by Example Fourth Edition>): My code is below: gawk -F'' ' { OFS = "****"; $3 = "(904)"; $8 = $5 + $6 + $7; print } ' The result is below: So, where is the $1? Why is the variable $8 located at the wired position? (3 Replies)
Discussion started by: franksunnn
3 Replies

5. Shell Programming and Scripting

[solved] awk: placement of user-defined functions

Hi folks, is there any recommendation, especially from a point of performance, about where to place a user-defined function in awk, like in BEGIN{} or if it is only need once at the end in END{}? Or doesn't it matter at all since, awk is so clever and only interprets it once, wherever it is... (3 Replies)
Discussion started by: zaxxon
3 Replies

6. Shell Programming and Scripting

User defined functions in awk

Hi; Is der ne to to use user defined functions for the values in awk find $1 -type f -ls | nawk '{{print "|"$3"|"$5"|"$6"|"$8"|"$9"|"$10"|"} for(i=11;i<=NF;i++){printf("%s",$i)}}' In above command i want to append some values returned by user functions on line. thnks; ajay (1 Reply)
Discussion started by: ajaypadvi
1 Replies

7. Shell Programming and Scripting

Crontab jobs don't see variables defined in /etc/profile

Is it true that Crontab jobs don't see variables defined in /etc/profile? How to get around that? (4 Replies)
Discussion started by: proactiveaditya
4 Replies

8. Shell Programming and Scripting

using AWK printf with userdefine variables

seems simple but i've not been successfull in adding the value of a Variable to a every line of a file using AWK & printf i've come up with the following, but it's not working.:mad: --- mydatafile.dat e.g. of data in file: cau_actvty_fa_lrf.ksh fan_soco_fa.ksh ny_sum_lst.ksh... (4 Replies)
Discussion started by: danmauer
4 Replies

9. Shell Programming and Scripting

Return an array of strings from user defined function in awk

Hello Friends, Is it possible to return an array from a user defined function in awk ? example: gawk ' BEGIN{} { catch_line = my_function(i) print catch_line print catch_line print catch_line } function my_function(i) { print "echo" line= "awk" line= "gawk"... (2 Replies)
Discussion started by: user_prady
2 Replies

10. Programming

pre defined variables

Hi First I want to explain the scenerio : In my project I want to control the printing of some messages. Thats why I use #ifdef MESSAGE ------------print message-------------- #endif And when I compile the program with -DMESSAGE option, it shows all message and without -DMESSAGE option... (7 Replies)
Discussion started by: sumsin
7 Replies
Login or Register to Ask a Question