How to use Awk to format numbers with a thousands separator
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
Can anyone tell me how to use the awk command for this statement "the number will contain commas after every three digits. For example, 100000000 will become 100,000,000."
I know how to use sed command to obtain this output but I don't know how to use awk, I'm beginner please tell me I'm searching from morning but I didn't get a suitable answer for this so please suggest me best tutorials for learning.
sed command to get the above output:
echo "100000000" | sed -E 's/([0-9]3)/,1/2g'
text-processing awk text-formatting
add a comment |Â
up vote
7
down vote
favorite
Can anyone tell me how to use the awk command for this statement "the number will contain commas after every three digits. For example, 100000000 will become 100,000,000."
I know how to use sed command to obtain this output but I don't know how to use awk, I'm beginner please tell me I'm searching from morning but I didn't get a suitable answer for this so please suggest me best tutorials for learning.
sed command to get the above output:
echo "100000000" | sed -E 's/([0-9]3)/,1/2g'
text-processing awk text-formatting
What search term are you using? Try "thousands separator". FYI GNU awk has a printf format modifier
â steeldriver
Dec 13 '15 at 18:22
2
Yoursed
command doesn't do what you think. It only works with 9-digit numbers...
â don_crissti
Dec 13 '15 at 18:28
search term stands for "to search on internet to resolve this"
â Error3735
Dec 14 '15 at 18:27
Alright! i have seen it'll work just for 9 digit numbers and i want a same output for same numbers but by using the awk can you help me?
â Error3735
Dec 14 '15 at 18:29
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
Can anyone tell me how to use the awk command for this statement "the number will contain commas after every three digits. For example, 100000000 will become 100,000,000."
I know how to use sed command to obtain this output but I don't know how to use awk, I'm beginner please tell me I'm searching from morning but I didn't get a suitable answer for this so please suggest me best tutorials for learning.
sed command to get the above output:
echo "100000000" | sed -E 's/([0-9]3)/,1/2g'
text-processing awk text-formatting
Can anyone tell me how to use the awk command for this statement "the number will contain commas after every three digits. For example, 100000000 will become 100,000,000."
I know how to use sed command to obtain this output but I don't know how to use awk, I'm beginner please tell me I'm searching from morning but I didn't get a suitable answer for this so please suggest me best tutorials for learning.
sed command to get the above output:
echo "100000000" | sed -E 's/([0-9]3)/,1/2g'
text-processing awk text-formatting
text-processing awk text-formatting
edited Dec 15 '15 at 12:02
nwk
653513
653513
asked Dec 13 '15 at 17:51
Error3735
5018
5018
What search term are you using? Try "thousands separator". FYI GNU awk has a printf format modifier
â steeldriver
Dec 13 '15 at 18:22
2
Yoursed
command doesn't do what you think. It only works with 9-digit numbers...
â don_crissti
Dec 13 '15 at 18:28
search term stands for "to search on internet to resolve this"
â Error3735
Dec 14 '15 at 18:27
Alright! i have seen it'll work just for 9 digit numbers and i want a same output for same numbers but by using the awk can you help me?
â Error3735
Dec 14 '15 at 18:29
add a comment |Â
What search term are you using? Try "thousands separator". FYI GNU awk has a printf format modifier
â steeldriver
Dec 13 '15 at 18:22
2
Yoursed
command doesn't do what you think. It only works with 9-digit numbers...
â don_crissti
Dec 13 '15 at 18:28
search term stands for "to search on internet to resolve this"
â Error3735
Dec 14 '15 at 18:27
Alright! i have seen it'll work just for 9 digit numbers and i want a same output for same numbers but by using the awk can you help me?
â Error3735
Dec 14 '15 at 18:29
What search term are you using? Try "thousands separator". FYI GNU awk has a printf format modifier
â steeldriver
Dec 13 '15 at 18:22
What search term are you using? Try "thousands separator". FYI GNU awk has a printf format modifier
â steeldriver
Dec 13 '15 at 18:22
2
2
Your
sed
command doesn't do what you think. It only works with 9-digit numbers...â don_crissti
Dec 13 '15 at 18:28
Your
sed
command doesn't do what you think. It only works with 9-digit numbers...â don_crissti
Dec 13 '15 at 18:28
search term stands for "to search on internet to resolve this"
â Error3735
Dec 14 '15 at 18:27
search term stands for "to search on internet to resolve this"
â Error3735
Dec 14 '15 at 18:27
Alright! i have seen it'll work just for 9 digit numbers and i want a same output for same numbers but by using the awk can you help me?
â Error3735
Dec 14 '15 at 18:29
Alright! i have seen it'll work just for 9 digit numbers and i want a same output for same numbers but by using the awk can you help me?
â Error3735
Dec 14 '15 at 18:29
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
10
down vote
A locale-independent solution with manual formatting
This should work on any POSIX-compatible OS regardless of the locales installed.
$ printf "1n10n100n1000n10000n100000n1000000n10000000n100000000n"
| awk ' len=length($0); res=""; for (i=0;i<=len;i++) res=substr($0,len-i+1,1) res; if (i > 0 && i < len && i % 3 == 0) res = "," res ; print res '
1
10
100
1,000
10,000
100,000
1,000,000
10,000,000
100,000,000
A solution that uses printf
and the en_US locale.
The printf
sequence %'d
prints a decimal integer formatted with the current locale's thousands separator.
$ printf "1n10n100n1000n10000n100000n1000000n10000000n100000000n"
| LC_ALL=en_US.UTF-8 awk ' printf("%'"'"'dn", $0) '
1
10
100
1,000
10,000
100,000
1,000,000
10,000,000
100,000,000
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
A locale-independent solution with manual formatting
This should work on any POSIX-compatible OS regardless of the locales installed.
$ printf "1n10n100n1000n10000n100000n1000000n10000000n100000000n"
| awk ' len=length($0); res=""; for (i=0;i<=len;i++) res=substr($0,len-i+1,1) res; if (i > 0 && i < len && i % 3 == 0) res = "," res ; print res '
1
10
100
1,000
10,000
100,000
1,000,000
10,000,000
100,000,000
A solution that uses printf
and the en_US locale.
The printf
sequence %'d
prints a decimal integer formatted with the current locale's thousands separator.
$ printf "1n10n100n1000n10000n100000n1000000n10000000n100000000n"
| LC_ALL=en_US.UTF-8 awk ' printf("%'"'"'dn", $0) '
1
10
100
1,000
10,000
100,000
1,000,000
10,000,000
100,000,000
add a comment |Â
up vote
10
down vote
A locale-independent solution with manual formatting
This should work on any POSIX-compatible OS regardless of the locales installed.
$ printf "1n10n100n1000n10000n100000n1000000n10000000n100000000n"
| awk ' len=length($0); res=""; for (i=0;i<=len;i++) res=substr($0,len-i+1,1) res; if (i > 0 && i < len && i % 3 == 0) res = "," res ; print res '
1
10
100
1,000
10,000
100,000
1,000,000
10,000,000
100,000,000
A solution that uses printf
and the en_US locale.
The printf
sequence %'d
prints a decimal integer formatted with the current locale's thousands separator.
$ printf "1n10n100n1000n10000n100000n1000000n10000000n100000000n"
| LC_ALL=en_US.UTF-8 awk ' printf("%'"'"'dn", $0) '
1
10
100
1,000
10,000
100,000
1,000,000
10,000,000
100,000,000
add a comment |Â
up vote
10
down vote
up vote
10
down vote
A locale-independent solution with manual formatting
This should work on any POSIX-compatible OS regardless of the locales installed.
$ printf "1n10n100n1000n10000n100000n1000000n10000000n100000000n"
| awk ' len=length($0); res=""; for (i=0;i<=len;i++) res=substr($0,len-i+1,1) res; if (i > 0 && i < len && i % 3 == 0) res = "," res ; print res '
1
10
100
1,000
10,000
100,000
1,000,000
10,000,000
100,000,000
A solution that uses printf
and the en_US locale.
The printf
sequence %'d
prints a decimal integer formatted with the current locale's thousands separator.
$ printf "1n10n100n1000n10000n100000n1000000n10000000n100000000n"
| LC_ALL=en_US.UTF-8 awk ' printf("%'"'"'dn", $0) '
1
10
100
1,000
10,000
100,000
1,000,000
10,000,000
100,000,000
A locale-independent solution with manual formatting
This should work on any POSIX-compatible OS regardless of the locales installed.
$ printf "1n10n100n1000n10000n100000n1000000n10000000n100000000n"
| awk ' len=length($0); res=""; for (i=0;i<=len;i++) res=substr($0,len-i+1,1) res; if (i > 0 && i < len && i % 3 == 0) res = "," res ; print res '
1
10
100
1,000
10,000
100,000
1,000,000
10,000,000
100,000,000
A solution that uses printf
and the en_US locale.
The printf
sequence %'d
prints a decimal integer formatted with the current locale's thousands separator.
$ printf "1n10n100n1000n10000n100000n1000000n10000000n100000000n"
| LC_ALL=en_US.UTF-8 awk ' printf("%'"'"'dn", $0) '
1
10
100
1,000
10,000
100,000
1,000,000
10,000,000
100,000,000
edited Jun 14 at 5:33
Bruno Bronosky
1,85511112
1,85511112
answered Dec 13 '15 at 18:26
nwk
653513
653513
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f249116%2fhow-to-use-awk-to-format-numbers-with-a-thousands-separator%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
What search term are you using? Try "thousands separator". FYI GNU awk has a printf format modifier
â steeldriver
Dec 13 '15 at 18:22
2
Your
sed
command doesn't do what you think. It only works with 9-digit numbers...â don_crissti
Dec 13 '15 at 18:28
search term stands for "to search on internet to resolve this"
â Error3735
Dec 14 '15 at 18:27
Alright! i have seen it'll work just for 9 digit numbers and i want a same output for same numbers but by using the awk can you help me?
â Error3735
Dec 14 '15 at 18:29