hi all,
I had my script as
a=qw
b=rter
c=fdfd
curency=1000
printf"${curency} $a $b $c" > filename
can i have printf statement that can change the currency from 1000 to 1,000 like it should convert the number to currency format ..?(i.e for any number)
Depending on your locale, you can do something like this:
$ printf "%'d\n" 1000
1,000
1 Like
hi,
how can we include this %d tag in my prinf statement exactly like
a=qw
b=rter
c=fdfd
curency=1000
printf"${curency} $a $b $c" > filename
how to insert in the ablve printf statement..?
$ a=qw
$ b=rter
$ c=fdfd
$ currency=1000
$ LC_ALL=en_US.UTF8 printf "%'d %s %s %s\n" "$currency" "$a" "$b" "$c" > filename
$ cat filename
1,000 qw rter fdfd
hi
in awk command can we do the same..?
amount=2000
awk -F "," '{printf $amount}'
can u help me on this..?
---------- Post updated at 06:37 AM ---------- Previous update was at 06:32 AM ----------
hi jlliagre,
thanks for the reply.. the above statement is not working if the currency is at the middle
a=qw
b=rter
curency=1000
c=fdfd
printf"$a $b ${curency} $c" > filename
my requirement is it should be in the middle
Sure:
amount=1000
awk -v fmt="%'d\n" -v amount="$amount" 'BEGIN{printf fmt, amount}'
hi Scrutinizer,
i had my script as defined like my awk command is
amount=2000
awk -F "," '{printf $amount}'
i have to convert $amount to currency mine is a comma delimiter file aim getting value from that file like amount=$2
---------- Post updated at 07:11 AM ---------- Previous update was at 06:46 AM ----------
hi Scrutinizer,
as my awk command is -F (field sepaerator) but u wrote using -v but my awk command should have -F too is that possible ..?
Hi, hemanthsaikumar . Note that that the printf
statement has a format part as the first argument. You need to use that!
Also your awk statement cannot work like that because the content of the shell variable $amount
is unavailable to awk because of the single quotes..
Also note that I used the BEGIN section. You are using the middle section, so then awk expects a file to process.
Yes you can combine: awk -F, -v ...
hi ,
actually iam getting the amount value from other file which is a comma seperated file... liek i had a doubt like how to insert this printf statement in the awk command
awk -F "," '{printf $amount}'
for the above statemnt is der any posibilty of adding printf "%'2f\n" thing ...? to convert the amount ..?
Say the amount is in the 3rd field, you can do something like this:
awk '{$3=sprintf(curfmt,$3)}1' FS=, OFS=, curfmt="$%'.2f" file
or
awk '{printf fmt, $1, $2, $3, $4}' FS=, fmt="%s,%s,$%'.2f,%s\n" file
$ echo 0,2,10000,c | awk '{$3=sprintf(curfmt,$3)}1' FS=, OFS=, curfmt="$%'.2f"
0,2,$10,000.00,c
---
I specified the format as en external variable because of the single quote, which use gets complicated by the fact that the awk script itself is in single quotes. An alternative is to put the script in a separate file and call it with awk -f
BEGIN{
FS=OFS=","
}
{
$3=sprintf("$%'.2f",$3)
}
1
call it like
awk -f test.awk file
RudiC
August 26, 2014, 10:14am
11
Not all awk versions recognize that " ' " flag char, even when the shell's printf does.
drl
August 26, 2014, 11:19am
12
Hi.
An excerpt from the useful bash Cookbook: Solutions and Examples for bash Users (Cookbooks (O'Reilly)): Carl Albing, JP Vossen, Cameron Newham: 9780596526788: Amazon.com: Books
# cookbook filename: func_commify
# bash cookbook, Albig, et al, Page 450.
function commify {
typeset text=${1}
typeset bdot=${text%%.*}
typeset adot=${text#${bdot}}
typeset i commified
(( i = ${#bdot} - 1 ))
while (( i>=3 )) && [[ ${bdot:i-3:1} == [0-9] ]]; do
commified=",${bdot:i-2:3}${commified}"
(( i -= 3 ))
done
echo "${bdot:0:i+1}${commified}${adot}"
}
So once this shell function is available to the current shell, then this:
commify 1000
would produce this:
1,000
on a system like this:
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian 5.0.8 (lenny, workstation)
bash GNU bash 3.2.39
Best wishes ... cheers, drl
It should if it conforms to the POSIX.1-2008 standard...
The flag characters and their meanings are:
'
[CX] [Option Start] (The <apostrophe>.) The integer portion of the result of a decimal conversion ( %i, %d, %u, %f, %F, %g, or %G ) shall be formatted with thousands' grouping characters. For other conversions the behavior is undefined. The non-monetary grouping character is used. [Option End]
printf() function: DESCRIPTION
XBD Fileformat notation
awk: output statements
printf utility: EXTENDED DESCRIPTION
As noted earlier, it is however dependent on the locale if the non-monetary grouping character is defined...
$ LANG="en_US.UTF-8" awk -v fmt="%'d\n" -v amount=10000 'BEGIN{printf fmt, amount}'
10,000
$ LANG="C" awk -v fmt="%'d\n" -v amount=10000 'BEGIN{printf fmt, amount}'
10000
$ LANG="de_DE.UTF-8" awk -v fmt="%'d\n" -v amount=10000 'BEGIN{printf fmt, amount}'
10000
So it is probably advisable to use LANG="en_US.UTF-8" awk ..
where available..
RudiC
August 26, 2014, 12:21pm
14
That LANG=
doesn't necessarily help:
LANG="en_US.UTF-8" awk -v fmt="%'d\n" -v amount=10000 'BEGIN{printf fmt, amount}'
awk: run time error: improper conversion(number 1) in printf("%'d
")
FILENAME="" FNR=0 NR=0
, see also http://www.unix.com/302912434-post26.html
1 Like
rudic:
That LANG=
doesn't necessarily help:
LANG="en_US.UTF-8" awk -v fmt="%'d\n" -v amount=10000 'BEGIN{printf fmt, amount}'
awk: run time error: improper conversion(number 1) in printf("%'d
")
FILENAME="" FNR=0 NR=0
Does your locale -a
list en_US.UTF-8
?
SUS V2 is from 1997, so there is a difference with the POSIX.1-2008 standard...
Edit it appears to have been changed in the SUS V3 specifications (POSIX:2001). printf()
You are right that it should be used with caution though..