zoukankan      html  css  js  c++  java
  • linux中seq命令用法

    NAME
    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: )

    -w, --equal-width
    equalize width by padding with leading zeroes

    --help display this help and exit

    --version
    output version information and exit

    If FIRST or INCREMENT is omitted, it defaults to 1. That
    is, an omitted INCREMENT defaults to 1 even when LAST is
    smaller than FIRST. FIRST, INCREMENT, and LAST are inter-
    preted as floating point values. INCREMENT is usually pos-
    itive if FIRST is smaller than LAST, and INCREMENT is usu-
    ally negative if FIRST is greater than LAST. FORMAT must
    be suitable for printing one argument of type ‘double’; it
    defaults to %.PRECf if FIRST, INCREMENT, and LAST are all
    fixed point decimal numbers with maximum precision PREC,
    and to %g otherwise.

    用于产生从某个数到另外一个数之间的所有整数
    例一:
    # seq 1 10
    结果是1 2 3 4 5 6 7 8 9 10
    例二:
    #!/bin/bash
    for i in `seq 1 10`;
    do
    echo $i;
    done
    或者用
    for i in $(seq 1 10)
    也可以
    seq
    -f, --format=FORMAT      use printf style floating-point FORMAT (default: %g)
    -s, --separator=STRING   use STRING to separate numbers (default: )
    -w, --equal-width        equalize width by padding with leading zeroes
    -f 选项   指定格式
    #seq -f"%3g" 9 11
    9
    10
    11
    % 后面指定数字的位数 默认是"%g", 
    "%3g"那么数字位数不足部分是空格
    #sed -f"%03g" 9 11  这样的话数字位数不足部分是0
    % 前面制定字符串
    seq -f "str%03g" 9 11
    str009
    str010
    str011
    -w 指定输出数字同宽   不能和-f一起用
    seq -w -f"str%03g" 9 11
    seq: format string may not be specified when printing equal width strings
    seq -w 98 101
    098
    099
    100
    101
    输出是同宽的
    -s 指定分隔符  默认是回车
    seq -s" " -f"str%03g" 9 11
    str009 str010 str011
    要指定 做为分隔符号
    seq -s"`echo -e " "`" 9 11  #如果要在终端中输出 或者 ,应该是:echo -e " " 或者 echo -e " "

    指定 作为分隔符号
    seq -s"`echo -e " "`" 9 11
    19293949596979899910911
    得到的是个错误结果
    不过一般也没有这个必要  它默认的就是回车作为分隔符
     
    几个例子
     
    awk 'BEGIN { while (num < 10 ) printf "dir%03d ", ++num ; exit}' | xargs mkdir
    mkdir $(seq -f 'dir%03g' 1 10)
     
    for i in `seq -f '%02g' 1 20`
    do
    if ! wget -P $HOME/tmp -c [img]http://www.xxxsite.com/photo/$i.jpg[/img] ; then
    wget -P $HOME/tmp -c $_
    fi
    done
     
     
    seq 是Linux 中一個預設的外部命令,一般用作一堆數字的簡化寫法,如
    seq 1 10
    便會出現
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    它還有三個選項
    -f, --format=FORMAT      use printf style floating-point FORMAT (default: %g)
    -s, --separator=STRING   use STRING to separate numbers (default: )
    -w, --equal-width        equalize width by padding with leading zeroes
    -f 最常用, 例如一次制做10 個名dir001 , dir002 .. dir010 的目錄,它便很有用途,我們可以
    這樣下一個命令便可了
    seq -f 'dir%03g' 1 10 | xargs mkdir

    mkdir $(seq -f 'dir%03g' 1 10)
    它用的是printf 的格式, %03g' 代表以三位浮點數,以此方法,如用bash3 的printf
    也可作為等價命令
    printf 'dir%03d ' {1..10} | xargs mkdir  或mkdir `printf 'dir%03d ' {1..10}`
    awk 當然也可以
    awk 'BEGIN { while (num < 10 ) printf "dir%03d ", ++num ; exit}' | xargs mkdir
    這樣會比寫一個腳本快, 不必寫成
    for dir in 001 002 003 004 005 006 007 008 009 010
    do
    mkdir dir${dir}
    done
    我也常用seq 下載用數字的jpeg , 只要格式有數字順序便可,尤以一些xxx site
    for i in `seq -f '%02g' 1 20`
    do
    if ! wget -P $HOME/tmp -c [img]http://www.xxxsite.com/photo/$i.jpg[/img] ; then
    wget -P $HOME/tmp -c $_
    fi
    done
     
    -s 選項主要改變輸出的分格符, 預設是 , 就是newline
    如用-s 便可改變, 如
    seq -s ' ' 1 10
    1 2 3 4 5 6 7 8 9 10  , 以空格作為分格, 但在Gnu 的seq , 好像
    不支援 , ...等字符? 如用 , 以兩個空格, 便得寫成
    [victor@localhost ~]$ seq -s '

    > ' 1 5
    1
     
    2
     
    3
     
    4
     
    5
    便得改變IFS, 如用
    OIFS=$IFS
    IFS=" "
    seq -s `echo -e $IFS` 1 5
    IFS=$OIFS
    其它的字符也是這樣吧?
     
     
    seq命令的作用就是打印出一串有序的数字,它主要有以下3个参数构成:
     
           -f, --format=FORMAT
    use printf style floating-point FORMAT (default: %g)
    -f 指定打印的格式:
    例如:
    [root@hao32]# seq -f %05g 2 7
    00002
    00003
    00004
    00005
    00006
    00007
     
           -s, --separator=STRING
    use STRING to separate numbers (default: )
    -s 指定分隔符 默认是回车:
    例如:
    [root@hao32]# seq -s" " 2 7
    2 3 4 5 6 7
     
           -w, --equal-width
    equalize width by padding with leading zeroes
    -w 输出是同宽 前面不足的用"0" 补全,即与位数最多的数对齐
    例如:
    [root@hao32]# seq -w 2 11
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11

  • 相关阅读:
    Windows性能计数器应用
    Azure Oracle Linux VNC 配置
    Azure 配置管理系列 Oracle Linux (PART6)
    Azure 配置管理系列 Oracle Linux (PART5)
    Azure 配置管理系列 Oracle Linux (PART4)
    Azure 配置管理系列 Oracle Linux (PART3)
    Azure 配置管理系列 Oracle Linux (PART2)
    vagrant多节点配置
    docker基本操作
    LINUX开启允许对外访问的网络端口命令
  • 原文地址:https://www.cnblogs.com/yi-meng/p/seq.html
Copyright © 2011-2022 走看看