Add leading zeroes to awk variable

半腔热情 提交于 2019-11-29 07:11:12

问题


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 

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.

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


回答1:


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

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




回答2:


Here's how to create leading zeros with awk:

# echo 1 | awk '{ printf("%02d\n", $1) }'
01
# echo 21 | awk '{ printf("%02d\n", $1) }'
21

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




回答3:


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.

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

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

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

The result in both cases is 00000722.




回答4:


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

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($1,$2,$3)
}

Some tests:

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

The test:

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

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



来源:https://stackoverflow.com/questions/7182075/add-leading-zeroes-to-awk-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!