Variable inside AWK printf


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable inside AWK printf
# 1  
Old 07-09-2011
Variable inside AWK printf

Hi,

Is it possible to print a variable inside awk printf ??

Quote:
nawk -v date="2011" ' BEGIN {FS=","} {printf("%-1s%-4s%2s%-4s","0",$2,date,$3)}'
the above is not working
# 2  
Old 07-09-2011
What is the problem ?
The statement seems valid.


Jean-Pierre.
# 3  
Old 07-09-2011
Am sorry when i give more than one variable am getting an error

Code:
 
cat $FILENAME |grep ^1 | while read line
do
echo "$line" | nawk -v FD="$F_D" SS="$S_S" DS="$D_S" 'BEGIN {FS=","} {printf("%-1s%-4s%-30s","0",$2,FD,SS,DS)}' >> HEAD
done

ERROR

HTML Code:
+ read line
+ cat testf
+ grep ^1
+ nawk -v FD=ALPHARMA_JDE                   SS=JDE_BRIDGE      DS=COMPUTRON    BEGIN {FS=","} {printf("%-1s%-4s%-30s","0",$2,FD,SS,DS)}
+ echo 1,2011,07,99999
+ 1>> HEAD
nawk: can't open file BEGIN {FS=","} {printf("%-1s%-4s%-30s","0",$2,FD,SS,DS)}
 source line number 1
+ read line
# 4  
Old 07-09-2011
1) Add -v options
Code:
nawk -v FD="$F_D" -v SS="$S_S" -v DS="$D_S" 'BEGIN {FS=","} {printf("%-1s%-4s%-30s","0",$2,FD,SS,DS)}'

2) Remove loop (cat,grep while read) :
Code:
nawk -v FD="$F_D" -v SS="$S_S" -v DS="$D_S" 'BEGIN {FS=","} /^1/ {printf("%-1s%-4s%-30s","0",$2,FD,SS,DS)}' $FILENAME

Jean-Pierre.
This User Gave Thanks to aigles For This Post:
# 5  
Old 07-09-2011
When specifying multiple variables, you need to put -v in front of each of them:
Code:
nawk -v FD="$F_D" -v SS="$S_S" -v DS="$D_S" 'BEGIN {FS=","} {printf("%-1s%-4s%-30s","0",$2,FD,SS,DS)}'

This User Gave Thanks to bartus11 For This Post:
# 6  
Old 07-09-2011
Thank you all... I have one more question
When i sum up the values of a column, i get the total in exponent, is it possible to convert in to a round fingure with 2 decimal places ?

Code:
awk -F, '{value=$28 ;if(value>0) {credit+=value}} END {print credit} ' joegtest_0705.txt

Code:
 
Output
1.20994e+07

# 7  
Old 07-09-2011
Use printf "%.2f",credit in the END section.
This User Gave Thanks to bartus11 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Variable and awk inside for loop

Thanks all for taking time out and reading this thread and big Thanks to all who have come forward for rescue. Background: I have a variable "nbrofcols" that has number of columns from a data file. Now, using this count in for loop, I am trying to get the maximum length of each column present... (7 Replies)
Discussion started by: svks1985
7 Replies

2. Shell Programming and Scripting

Variable assignment inside awk

Hi, Was hoping someone could help with the following: while read line; do pntadm -P $line | awk '{if (( $2 == 00 && $1 != 00 ) || ( $2 == 04 )) print $3,$5}'; done < /tmp/subnet_list Anyone know if it is possible to assign $3 and $5 to separate variables within the {} brackets? Thanks... (14 Replies)
Discussion started by: CiCa
14 Replies

3. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

Problem using variable inside awk

HI, This is the code I am using: awk -v aaa="connect" 'BEGIN {IGNORECASE} /aaa/,/!/ {print NR}' bb This does not throw any error but it does not work. Pls help Thanks. (4 Replies)
Discussion started by: sudvishw
4 Replies

6. Shell Programming and Scripting

Using variable inside awk

Hi, Please help me how to use variables inside awk in code below: ll | awk -v "yr=`date '+%Y'`" -v "mnth=`date '+%m'`" -v Jan=1 -v Feb=2 -v Mar=3 -v Apr=4 -v May=5 -v Jun=6 -v Jul=7 -v Aug=8 ' !/^d/ { if(NR>1) {printf "%-29s\t\t%s\t%5s\t\t%s %s,", $9,$1,$5,$`$6`,$7} }' Thanks. (10 Replies)
Discussion started by: manubatham20
10 Replies

7. Shell Programming and Scripting

How to use same variable value inside as well as outside of the awk command?

Hi Jim, The following script is in working state. But i m having one more problem with awk cmd. Could you tell me how to use any variable inside awk or how to take any variable value outside awk. My problem is i want to maintain one property file in which i am declaring variable value into that... (12 Replies)
Discussion started by: Ganesh Khandare
12 Replies

8. Shell Programming and Scripting

Using variable inside awk

I am trying to print the lines with pattern and my pattern is set to a variable express awk '/$express/{where=NR;print}' test2.log I am not getting any data even though i have the data with the pattern. Can seomeone correct me with the awk command above? (20 Replies)
Discussion started by: rdhanek
20 Replies

9. Shell Programming and Scripting

getting variable inside awk

Hi All, I have awk script for replacing the nth ocurance of a string in an xml file... My code is like this FILETYPE=xml TAGNAME=type OCCURANCE=$1 TAGVALUE=valueur echo OCCURANCE:$OCCURANCE echo TAGNAME:$TAGNAME echo TAGVALUE:$TAGVALUE awk -v n=$OCCURANCE -v... (1 Reply)
Discussion started by: subin_bala
1 Replies

10. Shell Programming and Scripting

awk: assign a printf value to a variable

Is there any way to something like this?: variable=printf("%30s",var1) Thx. (2 Replies)
Discussion started by: Klashxx
2 Replies
Login or Register to Ask a Question