当前位置: 首页> 教育> 锐评 > Linux seq命令

Linux seq命令

时间:2025/7/18 1:24:26来源:https://blog.csdn.net/feyehong/article/details/142030438 浏览次数:0次

参考资料

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

目录

  • 一. 基本语法
  • 二. 选项
    • 2.1 -f 格式化输出
    • 2.2 -s 指定分隔符
    • 2.3 -w 输出数字补齐宽度
  • 三. 小案例
    • 3.1 递减序列
    • 3.2 批量创建测试文件
    • 3.3 批量下载文件


一. 基本语法

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
关键字:Linux seq命令

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: