bash 将前导零添加到 awk 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7182075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:58:05  来源:igfitidea点击:

Add leading zeroes to awk variable

bashvariablesawkzeropad

提问by mirix

I have the following awk command within a "for" loop in bash:

我在 bash 的“for”循环中有以下 awk 命令:

awk -v pdb="$pdb" 'BEGIN {file = 1; filename = pdb"_" file ".pdb"}
 /ENDMDL/ {getline; file ++; filename = pdb"_" file ".pdb"}
 {print 
# echo 1 | awk '{ printf("%02d\n", ) }'
01
# echo 21 | awk '{ printf("%02d\n", ) }'
21
> filename}' < ${pdb}.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.

这会读取一系列名为 $pdb.pdb 的文件,并将它们拆分为名为 $pdb_1.pdb、$pdb_2.pdb、...、$pdb_21.pdb 等的文件。但是,我想生成具有名称的文件像 $pdb_01.pdb, $pdb_02.pdb, ..., $pdb_21.pdb, 即向“file”变量添加填充零。

I have tried without success using printf in different ways. Help would be much appreciated.

我尝试以不同的方式使用 printf ,但没有成功。帮助将不胜感激。

回答by glglgl

Replace fileon output with sprintf("%02d", file).

file输出替换为sprintf("%02d", file).

Or even the whole assigment with filename = sprintf("%s_%02d.pdb", pdb, file);.

甚至整个分配与filename = sprintf("%s_%02d.pdb", pdb, file);.

回答by JJ.

Here's how to create leading zeros with awk:

以下是如何创建前导零awk

echo 722 8 | awk '{ for(c = 0; c < ; c++) s = s"0"; s = s; print substr(s, 1 + length(s) - ); }'

Replace %02with the total number of digits you need (including zeros).

替换%02为您需要的总位数(包括零)。

回答by ThomasMcLeod

This does it without resort of printf, which is expensive. The first parameter is the string to pad, the second is the total length after padding.

这样做无需借助printf,这是昂贵的。第一个参数是要填充的字符串,第二个参数是填充后的总长度。

echo 722 | awk '{ s = "00000000"; print substr(s, 1 + length(s) - 8); }'

If you know in advance the length of the result string, you can use a simplified version (say 8 is your limit):

如果您事先知道结果字符串的长度,则可以使用简化版本(例如 8 是您的限制):

function zeropad(s,c,d) {
    if(d!="r")             
        d="l"                # l is the default and fallback value
    return sprintf("%" (d=="l"? "0" c:"") "d" (d=="r"?"%0" c-length(s) "d":""), s,"")
}
{                            # test main
    print zeropad(,,)
}

The result in both cases is 00000722.

两种情况下的结果都是00000722

回答by James Brown

Here is a function that left or right-pads values with zeroes depending on the parameters: zeropad(value, count, direction)

这是一个函数,它根据参数向左或向右填充零值:zeropad(value, count, direction)

$ cat test
2 3 l
2 4 r
2 5
a 6 r

Some tests:

一些测试:

$ awk -f program.awk test
002
2000
00002
000000

The test:

考试:

##代码##

It's not fully battlefield tested so strange parameters may yield strange results.

它没有经过全面的战场测试,所以奇怪的参数可能会产生奇怪的结果。