【问题标题】:Adding padded zeros to name files [duplicate]在名称文件中添加填充零 [重复]
【发布时间】:2020-12-15 12:30:29
【问题描述】:

基于此:How to zero pad a sequence of integers in bash so that all have the same width?

我需要创建新的文件名以输入代表染色体 1-22 的三位数数组 (chromsome001_results_file.txt..chromsome022_results_file.txt)

在使用三位数系统(排序更容易)之前,我正在使用

for i in {1..22}; 
do echo chromsome${i}_results_file.txt; 
done 

我已阅读有关 printf 和 seq 的信息,但想知道如何将它们放在被文本包围的循环中间,以使 001 到 022 与文本保持一致。

非常感谢

【问题讨论】:

  • for i in {001..022}; do
  • 您链接到的问题足以解决这个问题。

标签: bash loops for-loop printf seq


【解决方案1】:

使用printf 指定一个带零填充的字段。

for i in {1..22}; 
do 
    printf 'chromsome%03d_results_file.txt\n' "$i"
done 

%03d 中,d 表示十进制输出,3 表示 3 位数字,0 表示零填充。

【讨论】:

    猜你喜欢
    • 2018-10-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 2011-03-28
    • 2019-03-15
    • 2015-12-27
    相关资源
    最近更新 更多