Linux seq命令

参考资料

  1. 知っておくとちょっと便利!seq コマンドの使い方をご紹介


一. 基本语法

seq [OPTION] 结束值
seq [OPTION] 起始值 结束值
seq [OPTION] 起始值 步长 结束值

OPTION

  • -f:指定输出格式
    • %.2f:浮点数格式,保留2位小数。
    • %05g:前头补零位数,1会处理为000001
  • -s:指定分隔符,默认为换行符
  • -w:为输出的数字补全宽度,保持一致。
    例如输出1到10,最大数字为两位,则最终输出01到10

二. 选项

2.1 -f 格式化输出

⏹保留2位小数

fengyehong@ubuntu:~$ seq -f "%.2f" 1 0.5 3
1.00
1.50
2.00
2.50
3.00

⏹前头数字补齐到3位

fengyehong@ubuntu:~$ seq -f "%05g" 1 5
00001
00002
00003
00004
00005

⏹格式化输出文件名

# 方式1:多个命令配合,较为繁琐
fengyehong@ubuntu:~$ seq -f "%02g" 1 6 | xargs -I {} echo "host{}_info.txt"
host01_info.txt
host02_info.txt
host03_info.txt
host04_info.txt
host05_info.txt
host06_info.txt

# 方式2:直接通过seq命令格式化输出
fengyehong@ubuntu:~$ seq -f "host%02g_info.txt" 1 6
host01_info.txt
host02_info.txt
host03_info.txt
host04_info.txt
host05_info.txt
host06_info.txt

2.2 -s 指定分隔符

  • 默认情况下,分隔符是换行符
  • 通过-s指定分隔符是换行符之外的符号时,可在一行输出
fengyehong@ubuntu:~$ seq 1 5
1
2
3
4
5

# 通过指定分隔符为逗号,从而实现一行显示
fengyehong@ubuntu:~$ seq -s ',' 1 10
1,2,3,4,5,6,7,8,9,10

2.3 -w 输出数字补齐宽度

⏹起始值和结束值的长度一样,此时-w无效果

fengyehong@ubuntu:~$ seq -w 1 5
1
2
3
4
5

⏹起始值和结束值的长度不同,以长度最长的结束值的长度为基准补齐

fengyehong@ubuntu:~$ seq -w 1 10
01
02
03
04
05
06
07
08
09
10

三. 小案例

3.1 递减序列

⏹生成从10到2的递减序列(步长为2)

fengyehong@ubuntu:~$ seq 10 -2 2
10
8
6
4
2

3.2 批量创建测试文件

⏹批量创建1到12的测试文件(步长为2)

fengyehong@ubuntu:~$ seq -w 1 2 12 | xargs -t -I {} touch file_{}.txt
touch file_01.txt
touch file_03.txt
touch file_05.txt
touch file_07.txt
touch file_09.txt
touch file_11.txt

3.3 批量下载文件

  • 配合xragswget 命令批量下载文件
  • -n 1:每一项作为一个参数传入
  • --max-procs=3:开启3线程
fengyehong@ubuntu:~$ seq 1 10 | xargs -p -n 1 --max-procs=3 -I {} wget http://example.com/file{}
wget http://example.com/file1 ?...n
wget http://example.com/file2 ?...n
wget http://example.com/file3 ?...n
wget http://example.com/file4 ?...n
wget http://example.com/file5 ?...n
wget http://example.com/file6 ?...n
wget http://example.com/file7 ?...n
wget http://example.com/file8 ?...n
wget http://example.com/file9 ?...n
wget http://example.com/file10 ?...n
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值