您的位置:首页 > 运维架构 > Linux

linux命令之seq

2016-05-24 13:59 459 查看

seq命令简述

seq命令比较常用,在需要做循环的时候用于产生一个序列是再合适不过的工具了,常用方法也比较简单:
Usage:
seq [OPTION]... LAST
seq [OPTION]... FIRST LAST
seq [OPTION]... FIRST INCREMENT LAST
Print numbers from FIRST to LAST, in steps of INCREMENT.
# 以INCREMENT为步长打印从FIRST到LAST的数字

其参数也比较简单,主要有以下几个:
# 指定格式
-f, --format=FORMAT use printf style floating-point FORMAT
# 指定分隔符
-s, --separator=STRING use STRING to separate numbers (default: \n)
# 等宽参数,在日常工作中非常有用。
-w, --equal-width equalize width by padding with leading zeroes

基本用法

默认的分隔符为换行(\n)

thatsit:~ # seq 1 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
thatsit:~ #


# 指定分隔符为空格

thatsit:~ # seq -s " " 1 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
thatsit:~ #


# 使用等宽参数

thatsit:~ # seq -w -s " " 1 20
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20
thatsit:~ #


高级用法

高级用法主要在于-f参数的使用上,具体使用帮助可以通过info seq来获取。
可以通过printf指定format来输出两位精度的浮点数、十进制(默认)、十六进制、八进制等的序列:

下面以seq 1 20为例进行示范(printf中用一个空格来分隔数据):
十进制(默认):

thatsit:~ # printf '%g ' `seq 1 20`
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
thatsit:~ #


八进制:

thatsit:~ # printf '%o ' `seq 1 20`
1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20 21 22 23 24
thatsit:~ #


十六进制:

thatsit:~ # printf '%x ' `seq 1 20`
1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13 14
thatsit:~ #


两位数精度的浮点数:

thatsit:~ # printf '%.2f ' `seq 1 20`
1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 10.00 11.00 12.00 13.00 14.00 15.00 16.00 17.00 18.00 19.00 20.00
thatsit:~ #


四位数精度的浮点数:

thatsit:~ # printf '%.4f ' `seq 1 20`
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000
thatsit:~ #


others

此外还有一些科学计数法等格式,一般用不到,了解下即可:

如:%e、%E、a%、A%等
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: