bash 序列 00 01 ... 10
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11891162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash sequence 00 01 ... 10
提问by JuanPablo
in bash, with
在 bash 中,与
$ echo {1..10}
1 2 3 4 5 6 7 8 9 10
I can get a numbers sequence, but in some case I need
我可以得到一个数字序列,但在某些情况下我需要
01 02 03 ... 10
how I can get this ?
我怎么能得到这个?
and how I can get ?
我怎么能得到?
001 002 ... 010 011 .. 100
回答by favoretti
This will work in any shell on a machine that has coreutils
installed (thanks commenters for correcting me):
这将在已coreutils
安装的机器上的任何 shell 中工作(感谢评论者纠正我):
seq -w 1 10
and
和
seq -w 1 100
回答by robin
use seq command with -f
parameter, try:
使用带-f
参数的seq 命令,尝试:
seq -f "%02g" 0 10
seq -f "%02g" 0 10
results: 00 01 02 03 04 05 06 07 08 09 10
结果:00 01 02 03 04 05 06 07 08 09 10
seq -f "%03g" 0 10
seq -f "%03g" 0 10
results: 000 001 002 003 004 005 006 007 008 009 010
结果:000 001 002 003 004 005 006 007 008 009 010
回答by pb2q
printf "%02d " {1..10} ; echo
Output:
输出:
01 02 03 04 05 06 07 08 09 10
Similarly:
相似地:
printf "%03d " {1..100} ; echo
In more recent versions of bash, simply:
在较新版本的 bash 中,只需:
echo {01..10}
And:
和:
echo {001..100}
回答by lynxlynxlynx
$ printf "%02d " {0..10}; echo
00 01 02 03 04 05 06 07 08 09 10
$ printf "%03d " {0..100}; echo
000 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100
Just vary the field width in the format string (2 and 3 in this case) and of course the brace expansion range. The echo
is there just for cosmetic purposes, since the format string does not contain a newline itself.
只需改变格式字符串中的字段宽度(在本例中为 2 和 3),当然还有大括号扩展范围。该echo
有只是为了美容目的,因为格式字符串不包含换行符本身。
printf
is a shell builtin, but you likely also have a version from coreutils installed, which can be used in-place.
printf
是内置的 shell,但您可能还安装了 coreutils 的版本,可以就地使用。
回答by dimba
awk only:
仅 awk:
awk 'BEGIN { for (i=0; i<10; i++) printf("%02d ", i) }'
回答by Florin Stingaciu
The following will work in bash
以下将在 bash 中工作
echo {01..10}
**EDIT seeing the answers around me I just wanted to add this, in the case we're talking about commands that will work under any terminal
**编辑看到我周围的答案我只想添加这个,如果我们谈论的是可以在任何终端下工作的命令
yes | head -n 100 | awk '{printf( "%03d ", NR )}' ##for 001...100
or
或者
yes | head -n 10 | awk '{printf( "%03d ", NR )}' ##for 01..10
回答by William Pursell
There are so many ways to do this! My personal favorite is:
有很多方法可以做到这一点!我个人最喜欢的是:
yes | grep y | sed 100q | awk '{printf( "%03d ", NR )}'; echo
Clearly, neither the sed
nor the grep
are necessary (the grep
being far more trivial, since if you omit the sed
you need to change the awk
), but they contribute to the overall satisfaction of the solution! The final echo
is not really necessary either, but it's always nice to have a trailing newline.
显然, thesed
和 thegrep
都不是必需的(grep
因为如果您省略 thesed
您就需要更改 the awk
),但它们有助于解决方案的整体满意度!finalecho
也不是真正必要的,但是有一个尾随换行符总是很好的。
Another nice option is:
另一个不错的选择是:
yes | nl -ba | tr ' ' 0 | sed 100q | cut -b 4-6
Or (less absurdly):
或者(不那么荒谬):
yes '' | sed ${top-100}q | nl -ba -w ${width-3} -n rz
回答by runlevel0
as commented by favoretti, seq
is your friend.
正如喜欢的评论,seq
是你的朋友。
But there is a caveat:
但有一个警告:
seq -w
uses the second argument to set the format it will use.
seq -w
使用第二个参数来设置它将使用的格式。
Thus, the command seq -w 1 9
will print the sequence 1 2 3 4 5 6 7 8 9
因此,该命令seq -w 1 9
将打印序列1 2 3 4 5 6 7 8 9
To print the sequence 01 .. 09 you need to do the following:
要打印序列 01 .. 09,您需要执行以下操作:
seq -w 1 09
seq -w 1 09
Or for clarities sake use the same format on both ends, for instance:
或者为了清楚起见,在两端使用相同的格式,例如:
seq -w 000 010
for the series 001 002 003 ... 010
seq -w 000 010
对于系列 001 002 003 ... 010
And you can also use a step argument that also works in reverse:
您还可以使用也可以反向工作的 step 参数:
seq -w 10 -1 01' for 10,09,08...01 or
seq -w 01 2 10` for 01,03,05,07,09
seq -w 10 -1 01' for 10,09,08...01 or
seq -w 01 2 10` 为 01,03,05,07,09
回答by Shuaishuai Yuan
echo 0{0..9}
You can get: 00 01 02 03 04 05 06 07 08 09
您可以获得:00 01 02 03 04 05 06 07 08 09
echo 0{0..9} 1{0..9}
You can get: 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19
你可以得到: 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19
echo 00{0..9} 0{10..99}
You can get 001 .. 099
你可以得到 001 .. 099