我正在尝试将该awkscript转换为python,而这是我不熟悉的语法。我知道else语句之后的那一部分正在设置用于输出到文件的空格填充,但是if / else使我感到困惑。
if (substr($36,1,1) ~ /^[0-9]+$/) {
C = $36;
CLen = length($36);
}
else {
C = substr($36,2);
CLen = length(C);
}
CPad = 6 - CLen;
for (i = 1; i <= CPad; ++i) {
C = C " "
}
答案 0 :(得分:1)
请参见代码中的#
解释。
if (substr($36,1,1) ~ /^[0-9]+$/) {
# substr($36,1,1) => first char in input field #36
# ~ => is RegExp match to
# /^[0-9]+$/ => RegExp of : string consisting of 1 or more digits
C = $36; # Copy whole input field #36 to variable C
CLen = length($36); # Assign lenght of input field #36 to variable CLen
}
else {
C = substr($36,2); # Copy input field #36 from second char to end, into to variable C
CLen = length(C); # Assign length of variable C to variable CLen
}
CPad = 6 - CLen; # Compute CPad
for (i = 1; i <= CPad; ++i) {
C = C " " # pad CPad spaces to C right side.
}
祝你好运。
答案 1 :(得分:0)
请注意,这是substr和regex匹配的无用组合
substr($36,1,1) ~ /^[0-9]+$/
相当于$36 ~ /^[0-9]/
也可以用右填充6个字符
c=sprintf("%-6s",c)
总而言之,如果第一个字符为数字,则将其删除,右填充至6个字符长。也许这样更容易...
sub(/^[0-9]/,"",$36); c=sprintf("%-6s",$36)