1

Linux 之 seq 命令

 2 years ago
source link: https://segmentfault.com/a/1190000040580669
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

以指定增量从首数开始打印数字到尾数,seq命令用于产生从某个数到另外一个数之间的所有整数。

> seq [选项]... 尾数
> seq [选项]... 首数 尾数
> seq [选项]... 首数 增量 尾数
  • -f, --format=格式 使用printf 样式的浮点格式
  • -s, --separator=字符串 使用指定字符串分隔数字(默认使用:\n)
  • -w, --equal-width 在列前添加0 使得宽度相同

指定格式显示

> seq -f"%3g" 10
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

%后面指定数字的位数默认是%g%3g那么数字位数不足部分默认是空格。

指定格式显示,不足的位数填充0

> seq -f"%03g" 10
001
002
003
004
005
006
007
008
009
010

指定3位显示,不足3位填充零

指定格式显示,不足的位数填充0,并在前面添加字符串

> seq -f"rumen%03g" 10
rumen001
rumen002
rumen003
rumen004
rumen005
rumen006
rumen007
rumen008
rumen009
rumen010

这样的话数字位数不足部分是0,%前面制定字符串。

指定输出数字同宽

> seq -w 10
01
02
03
04
05
06
07
08
09
10

不能和-f一起用,输出是同宽的。

指定分隔符(默认是回车)

> seq -s" " 10
1 2 3 4 5 6 7 8 9 10
> seq -s"#" 10
1#2#3#4#5#6#7#8#9#10

要指定/t做为分隔符号

> seq -s"`echo -e "\t"`" 10
1       2       3       4       5       6       7       8       9       10

批量创建文件

> touch $(seq -f"rumenz%03g.txt" 10)
> ls -al *.txt
-rw-r--r-- 1 root root   0 Apr  4 22:46 rumenz001.txt
-rw-r--r-- 1 root root   0 Apr  4 22:46 rumenz002.txt
-rw-r--r-- 1 root root   0 Apr  4 22:46 rumenz003.txt
-rw-r--r-- 1 root root   0 Apr  4 22:46 rumenz004.txt
-rw-r--r-- 1 root root   0 Apr  4 22:46 rumenz005.txt
-rw-r--r-- 1 root root   0 Apr  4 22:46 rumenz006.txt
-rw-r--r-- 1 root root   0 Apr  4 22:46 rumenz007.txt
-rw-r--r-- 1 root root   0 Apr  4 22:46 rumenz008.txt
-rw-r--r-- 1 root root   0 Apr  4 22:46 rumenz009.txt
-rw-r--r-- 1 root root   0 Apr  4 22:46 rumenz010.txt

以倒序显示数字

> eq 10 -1 1
10
9
8
7
6
5
4
3
2
1
> seq -f "%f" 1 0.5 3
1.000000
1.500000
2.000000
2.500000
3.000000

-f小数显示,步长是0.5

小数序列,指定小数位数

> seq -f "%.2f" 1 0.5 3
1.00
1.50
2.00
2.50
3.00

%.2f显示2位小数,步长是0.5

原文链接:https://rumenz.com/rumenbiji/...
微信公众号:入门小站


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK