seq命令,打印数字序列。
seq - print a sequence of numbers
SYNOPSIS
seq [OPTION]... LAST
seq [OPTION]... FIRST LAST
seq [OPTION]... FIRST INCREMENT LAST
DESCRIPTION
Print numbers from FIRST to LAST, in steps of INCREMENT.
-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
--help display this help and exit
--version
output version information and exit
参数:-f,-s,-w,-help
-f:格式化输出浮点数
[[email protected] ~]# seq -f "%0.3f" 5 1.000 2.000 3.000 4.000 5.000
使用-f参数时得注意特定匹配,必须以%开头,且,%必须和efgaEFGA等字符成对出现,不然,命令执行会报错。
[[email protected] ~]# seq -f "%5.3e" 3 1.000e+00 2.000e+00 3.000e+00 [[email protected] ~]# seq -f "%e" 3 1.000000e+00 2.000000e+00 3.000000e+00
格式化输入处理源码如下:
/* If FORMAT is a valid printf format for a double argument, return its long double equivalent, allocated from dynamic storage, and store into *LAYOUT a description of the output layout; otherwise, report an error and exit. */ static char const * long_double_format (char const *fmt, struct layout *layout) { size_t i; size_t prefix_len = 0; size_t suffix_len = 0; size_t length_modifier_offset; bool has_L; for (i = 0; ! (fmt[i] == ‘%‘ && fmt[i + 1] != ‘%‘); i += (fmt[i] == ‘%‘) + 1) { if (!fmt[i]) error (EXIT_FAILURE, 0, _("format %s has no %% directive"), quote (fmt)); prefix_len++; } i++; i += strspn (fmt + i, "-+#0 ‘"); i += strspn (fmt + i, "0123456789"); if (fmt[i] == ‘.‘) { i++; i += strspn (fmt + i, "0123456789"); } length_modifier_offset = i; has_L = (fmt[i] == ‘L‘); i += has_L; if (fmt[i] == ‘\0‘) error (EXIT_FAILURE, 0, _("format %s ends in %%"), quote (fmt)); if (! strchr ("efgaEFGA", fmt[i])) error (EXIT_FAILURE, 0, _("format %s has unknown %%%c directive"), quote (fmt), fmt[i]); for (i++; ; i += (fmt[i] == ‘%‘) + 1) if (fmt[i] == ‘%‘ && fmt[i + 1] != ‘%‘) error (EXIT_FAILURE, 0, _("format %s has too many %% directives"), quote (fmt)); else if (fmt[i]) suffix_len++; else { size_t format_size = i + 1; char *ldfmt = xmalloc (format_size + 1); memcpy (ldfmt, fmt, length_modifier_offset); ldfmt[length_modifier_offset] = ‘L‘; strcpy (ldfmt + length_modifier_offset + 1, fmt + length_modifier_offset + has_L); layout->prefix_len = prefix_len; layout->suffix_len = suffix_len; return ldfmt; } }
-s:使用替他字符串做分隔符,默认分隔符是换行符
[[email protected] ~]# seq -s "w" 5
1w2w3w4w5
[[email protected] ~]# seq 5
1
2
3
4
5
[[email protected] ~]#
-w:数字显示宽度,不足的前面补0
[[email protected] ~]# seq -w 10 01 02 03 04 05 06 07 08 09 10 [[email protected] ~]# seq 10 1 2 3 4 5 6 7 8 9 10 [[email protected] ~]#
--help:显示帮助页
[[email protected] ~]# seq --help Usage: seq [OPTION]... LAST or: seq [OPTION]... FIRST LAST or: seq [OPTION]... FIRST INCREMENT LAST Print numbers from FIRST to LAST, in steps of INCREMENT. ……
时间: 2024-10-08 11:51:35