开发者

Add leading zeroes to awk variable

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 z开发者_如何学Ceros to the "file" variable.

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


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).


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

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜