添加前导零到awk的变量 [英] Add leading zeroes to awk variable
问题描述
我在下面的awk命令一个为在bash循环:
I have the following awk command within a "for" loop in bash:
awk -v pdb="$pdb" 'BEGIN {file = 1; filename = pdb"_" file ".pdb"}
/ENDMDL/ {getline; file ++; filename = pdb"_" file ".pdb"}
{print $0 > filename}' < ${pdb}.pdb
这读了一系列名为$ pdb.pdb文件,并分割他们在名为$ pdb_1.pdb,$ pdb_2.pdb,...,$ pdb_21.pdb等。不过,我想制作文件像$ pdb_01.pdb,$ pdb_02.pdb,...,$ pdb_21.pdb名称,即文件填充零添加到文件变量。
This reads a series of files with the name $pdb.pdb and splits them in files called $pdb_1.pdb, $pdb_2.pdb, ..., $pdb_21.pdb, etc. However, I would like to produce files with names like $pdb_01.pdb, $pdb_02.pdb, ..., $pdb_21.pdb, i.e., to add padding zeros to the "file" variable.
我曾尝试没有以不同的方式使用printf的成功。帮助将非常AP preciated。
I have tried without success using printf in different ways. Help would be much appreciated.
推荐答案
替换文件
与输出的sprintf(%02D,文件)
。
甚至整个assigment与 =文件名的sprintf(%S_%02d.pdb,PDB,文件);
Or even the whole assigment with filename = sprintf("%s_%02d.pdb", pdb, file);
.
这篇关于添加前导零到awk的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!