How to left pad a column in Bash

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have a bash script that prints the following results:
120,900
1160,001
80,730
600,165
6,310
1111,203
I would like to left pad that so the result will be:
120,900
1160,001
80,730
600,165
6,310
1111,203
I already use that line to keep just 3 numbers after the comma in the second column
awk 't" "%.3fn", $2 ' MyFile.txt;
How can I accomplish this?
scripting text-formatting whitespace
add a comment |Â
up vote
1
down vote
favorite
I have a bash script that prints the following results:
120,900
1160,001
80,730
600,165
6,310
1111,203
I would like to left pad that so the result will be:
120,900
1160,001
80,730
600,165
6,310
1111,203
I already use that line to keep just 3 numbers after the comma in the second column
awk 't" "%.3fn", $2 ' MyFile.txt;
How can I accomplish this?
scripting text-formatting whitespace
Please do not post pictures of text; just paste the text.
â DopeGhoti
May 9 at 16:10
1
If you have thatawksnippet in the script, you could probably add the right-alignment there, too, instead of doing it as a separate step in the shell.
â ilkkachu
May 9 at 18:44
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a bash script that prints the following results:
120,900
1160,001
80,730
600,165
6,310
1111,203
I would like to left pad that so the result will be:
120,900
1160,001
80,730
600,165
6,310
1111,203
I already use that line to keep just 3 numbers after the comma in the second column
awk 't" "%.3fn", $2 ' MyFile.txt;
How can I accomplish this?
scripting text-formatting whitespace
I have a bash script that prints the following results:
120,900
1160,001
80,730
600,165
6,310
1111,203
I would like to left pad that so the result will be:
120,900
1160,001
80,730
600,165
6,310
1111,203
I already use that line to keep just 3 numbers after the comma in the second column
awk 't" "%.3fn", $2 ' MyFile.txt;
How can I accomplish this?
scripting text-formatting whitespace
edited May 9 at 19:17
Jeff Schaller
31.1k846105
31.1k846105
asked May 9 at 16:07
Rania MALK
61
61
Please do not post pictures of text; just paste the text.
â DopeGhoti
May 9 at 16:10
1
If you have thatawksnippet in the script, you could probably add the right-alignment there, too, instead of doing it as a separate step in the shell.
â ilkkachu
May 9 at 18:44
add a comment |Â
Please do not post pictures of text; just paste the text.
â DopeGhoti
May 9 at 16:10
1
If you have thatawksnippet in the script, you could probably add the right-alignment there, too, instead of doing it as a separate step in the shell.
â ilkkachu
May 9 at 18:44
Please do not post pictures of text; just paste the text.
â DopeGhoti
May 9 at 16:10
Please do not post pictures of text; just paste the text.
â DopeGhoti
May 9 at 16:10
1
1
If you have that
awk snippet in the script, you could probably add the right-alignment there, too, instead of doing it as a separate step in the shell.â ilkkachu
May 9 at 18:44
If you have that
awk snippet in the script, you could probably add the right-alignment there, too, instead of doing it as a separate step in the shell.â ilkkachu
May 9 at 18:44
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
Use printf rather than echo:
$ cat 442817.sh
#!/bin/bash
numbers=(120,900 1160,001 80,730 600,165 6,310 1111,203)
for n in "$numbers[@]"; do
printf "%10sn" "$n"
done
$ ./442817.sh
120,900
1160,001
80,730
600,165
6,310
1111,203
1
or replace completeforloop withprintf "%8sn" "$numbers[@]"
â Cyrus
May 9 at 18:13
Also viable; the main thrust was to demonstrateprintf, as opposed toecho; I just wanted to "show my work" as it were.
â DopeGhoti
May 9 at 18:14
add a comment |Â
up vote
0
down vote
Awk solution:
awk 'FNR == NR len = length; if (len > max_len) max_len = len; next
printf "%" max_len "sn", $0 ' file.txt file.txt
The output:
120,900
1160,001
80,730
600,165
6,310
1111,203
Or via GNU coreutils:
printf "%$(sort -nr file.txt | wc -L)sn" $(cat file.txt)
add a comment |Â
up vote
0
down vote
$ column -t -s, file | sed 's/^([0-9]*)( *)/21,/'
120,900
1160,001
80,730
600,165
6,310
1111,203
The column output will be
120 900
1160 001
80 730
600 165
6 310
1111 203
and the sed command just moves the spaces between the two columns to the start of the line and inserts the comma in their place.
If the initial extra two spaces are not wanted, use
$ column -t -s, file | sed -e 's/^([0-9]*)( *)/21,/' -e 's/^ //'
120,900
1160,001
80,730
600,165
6,310
1111,203
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Use printf rather than echo:
$ cat 442817.sh
#!/bin/bash
numbers=(120,900 1160,001 80,730 600,165 6,310 1111,203)
for n in "$numbers[@]"; do
printf "%10sn" "$n"
done
$ ./442817.sh
120,900
1160,001
80,730
600,165
6,310
1111,203
1
or replace completeforloop withprintf "%8sn" "$numbers[@]"
â Cyrus
May 9 at 18:13
Also viable; the main thrust was to demonstrateprintf, as opposed toecho; I just wanted to "show my work" as it were.
â DopeGhoti
May 9 at 18:14
add a comment |Â
up vote
1
down vote
Use printf rather than echo:
$ cat 442817.sh
#!/bin/bash
numbers=(120,900 1160,001 80,730 600,165 6,310 1111,203)
for n in "$numbers[@]"; do
printf "%10sn" "$n"
done
$ ./442817.sh
120,900
1160,001
80,730
600,165
6,310
1111,203
1
or replace completeforloop withprintf "%8sn" "$numbers[@]"
â Cyrus
May 9 at 18:13
Also viable; the main thrust was to demonstrateprintf, as opposed toecho; I just wanted to "show my work" as it were.
â DopeGhoti
May 9 at 18:14
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Use printf rather than echo:
$ cat 442817.sh
#!/bin/bash
numbers=(120,900 1160,001 80,730 600,165 6,310 1111,203)
for n in "$numbers[@]"; do
printf "%10sn" "$n"
done
$ ./442817.sh
120,900
1160,001
80,730
600,165
6,310
1111,203
Use printf rather than echo:
$ cat 442817.sh
#!/bin/bash
numbers=(120,900 1160,001 80,730 600,165 6,310 1111,203)
for n in "$numbers[@]"; do
printf "%10sn" "$n"
done
$ ./442817.sh
120,900
1160,001
80,730
600,165
6,310
1111,203
edited May 9 at 18:39
ilkkachu
48.1k669133
48.1k669133
answered May 9 at 16:14
DopeGhoti
40k54779
40k54779
1
or replace completeforloop withprintf "%8sn" "$numbers[@]"
â Cyrus
May 9 at 18:13
Also viable; the main thrust was to demonstrateprintf, as opposed toecho; I just wanted to "show my work" as it were.
â DopeGhoti
May 9 at 18:14
add a comment |Â
1
or replace completeforloop withprintf "%8sn" "$numbers[@]"
â Cyrus
May 9 at 18:13
Also viable; the main thrust was to demonstrateprintf, as opposed toecho; I just wanted to "show my work" as it were.
â DopeGhoti
May 9 at 18:14
1
1
or replace complete
for loop with printf "%8sn" "$numbers[@]"â Cyrus
May 9 at 18:13
or replace complete
for loop with printf "%8sn" "$numbers[@]"â Cyrus
May 9 at 18:13
Also viable; the main thrust was to demonstrate
printf, as opposed to echo; I just wanted to "show my work" as it were.â DopeGhoti
May 9 at 18:14
Also viable; the main thrust was to demonstrate
printf, as opposed to echo; I just wanted to "show my work" as it were.â DopeGhoti
May 9 at 18:14
add a comment |Â
up vote
0
down vote
Awk solution:
awk 'FNR == NR len = length; if (len > max_len) max_len = len; next
printf "%" max_len "sn", $0 ' file.txt file.txt
The output:
120,900
1160,001
80,730
600,165
6,310
1111,203
Or via GNU coreutils:
printf "%$(sort -nr file.txt | wc -L)sn" $(cat file.txt)
add a comment |Â
up vote
0
down vote
Awk solution:
awk 'FNR == NR len = length; if (len > max_len) max_len = len; next
printf "%" max_len "sn", $0 ' file.txt file.txt
The output:
120,900
1160,001
80,730
600,165
6,310
1111,203
Or via GNU coreutils:
printf "%$(sort -nr file.txt | wc -L)sn" $(cat file.txt)
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Awk solution:
awk 'FNR == NR len = length; if (len > max_len) max_len = len; next
printf "%" max_len "sn", $0 ' file.txt file.txt
The output:
120,900
1160,001
80,730
600,165
6,310
1111,203
Or via GNU coreutils:
printf "%$(sort -nr file.txt | wc -L)sn" $(cat file.txt)
Awk solution:
awk 'FNR == NR len = length; if (len > max_len) max_len = len; next
printf "%" max_len "sn", $0 ' file.txt file.txt
The output:
120,900
1160,001
80,730
600,165
6,310
1111,203
Or via GNU coreutils:
printf "%$(sort -nr file.txt | wc -L)sn" $(cat file.txt)
edited May 9 at 16:32
answered May 9 at 16:22
RomanPerekhrest
22.4k12144
22.4k12144
add a comment |Â
add a comment |Â
up vote
0
down vote
$ column -t -s, file | sed 's/^([0-9]*)( *)/21,/'
120,900
1160,001
80,730
600,165
6,310
1111,203
The column output will be
120 900
1160 001
80 730
600 165
6 310
1111 203
and the sed command just moves the spaces between the two columns to the start of the line and inserts the comma in their place.
If the initial extra two spaces are not wanted, use
$ column -t -s, file | sed -e 's/^([0-9]*)( *)/21,/' -e 's/^ //'
120,900
1160,001
80,730
600,165
6,310
1111,203
add a comment |Â
up vote
0
down vote
$ column -t -s, file | sed 's/^([0-9]*)( *)/21,/'
120,900
1160,001
80,730
600,165
6,310
1111,203
The column output will be
120 900
1160 001
80 730
600 165
6 310
1111 203
and the sed command just moves the spaces between the two columns to the start of the line and inserts the comma in their place.
If the initial extra two spaces are not wanted, use
$ column -t -s, file | sed -e 's/^([0-9]*)( *)/21,/' -e 's/^ //'
120,900
1160,001
80,730
600,165
6,310
1111,203
add a comment |Â
up vote
0
down vote
up vote
0
down vote
$ column -t -s, file | sed 's/^([0-9]*)( *)/21,/'
120,900
1160,001
80,730
600,165
6,310
1111,203
The column output will be
120 900
1160 001
80 730
600 165
6 310
1111 203
and the sed command just moves the spaces between the two columns to the start of the line and inserts the comma in their place.
If the initial extra two spaces are not wanted, use
$ column -t -s, file | sed -e 's/^([0-9]*)( *)/21,/' -e 's/^ //'
120,900
1160,001
80,730
600,165
6,310
1111,203
$ column -t -s, file | sed 's/^([0-9]*)( *)/21,/'
120,900
1160,001
80,730
600,165
6,310
1111,203
The column output will be
120 900
1160 001
80 730
600 165
6 310
1111 203
and the sed command just moves the spaces between the two columns to the start of the line and inserts the comma in their place.
If the initial extra two spaces are not wanted, use
$ column -t -s, file | sed -e 's/^([0-9]*)( *)/21,/' -e 's/^ //'
120,900
1160,001
80,730
600,165
6,310
1111,203
answered Jun 5 at 15:42
Kusalananda
102k13199315
102k13199315
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%2f442817%2fhow-to-left-pad-a-column-in-bash%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
Please do not post pictures of text; just paste the text.
â DopeGhoti
May 9 at 16:10
1
If you have that
awksnippet in the script, you could probably add the right-alignment there, too, instead of doing it as a separate step in the shell.â ilkkachu
May 9 at 18:44