awk 變數無法在 for 迴圈內連接 substr() 傳回的字母“z”

awk 變數無法在 for 迴圈內連接 substr() 傳回的字母“z”

在使用密鑰為每個字母建立加密字元集時,該字母z似乎沒有添加到變數中。代碼有什麼問題嗎?請指出。

我嘗試z在 for 循環中添加額外的字元並增加計數器,但z沒有連接到字串。

BEGIN {
    charset="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    printf("Enter your choice:\n1. Encryption.\n2. Decryption.\n")
    getline choice < "-"
    print "Enter Key:"
    getline key < "-"
    for(i=1;i<=62;i++) {
            newch = substr(charset,(i+key)%62,1)
            encharset = encharset newch
            print encharset
    }
}

輸出

Enter your choice:
1. Encryption.
2. Decryption.
1
Enter Key:
19
J
JK
JKL
JKLM
JKLMN
JKLMNO
JKLMNOP
JKLMNOPQ
JKLMNOPQR
JKLMNOPQRS
JKLMNOPQRST
JKLMNOPQRSTU
JKLMNOPQRSTUV
JKLMNOPQRSTUVW
JKLMNOPQRSTUVWX
JKLMNOPQRSTUVWXY
JKLMNOPQRSTUVWXYZ
JKLMNOPQRSTUVWXYZa
JKLMNOPQRSTUVWXYZab
JKLMNOPQRSTUVWXYZabc
JKLMNOPQRSTUVWXYZabcd
JKLMNOPQRSTUVWXYZabcde
JKLMNOPQRSTUVWXYZabcdef
JKLMNOPQRSTUVWXYZabcdefg
JKLMNOPQRSTUVWXYZabcdefgh
JKLMNOPQRSTUVWXYZabcdefghi
JKLMNOPQRSTUVWXYZabcdefghij
JKLMNOPQRSTUVWXYZabcdefghijk
JKLMNOPQRSTUVWXYZabcdefghijkl
JKLMNOPQRSTUVWXYZabcdefghijklm
JKLMNOPQRSTUVWXYZabcdefghijklmn
JKLMNOPQRSTUVWXYZabcdefghijklmno
JKLMNOPQRSTUVWXYZabcdefghijklmnop
JKLMNOPQRSTUVWXYZabcdefghijklmnopq
JKLMNOPQRSTUVWXYZabcdefghijklmnopqr
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrs
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrst
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy   #from this line "z" is not appearing in the string
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy01
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy012
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy01234
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy012345
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy01234567
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy012345678
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789A
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789AB
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789ABC
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789ABCD
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789ABCDE
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789ABCDEF
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789ABCDEFG
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789ABCDEFGH
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789ABCDEFGHI
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789ABCDEFGHI

答案1

您沒有正確使用模運算子。

echo 0 62  | awk '{ print $1 % $2 }'     # "0"
echo 1 62  | awk '{ print $1 % $2 }'     # "1"
echo 61 62 | awk '{ print $1 % $2 }'     # "61"
echo 62 62 | awk '{ print $1 % $2 }'     # "0" not "62"

Yourz是字串的第 62 個字元charset。您正在計算以 62 為模的偏移量,這表示您的結果值可以在0...範圍內61。由於awk需要來自 的字串偏移量1,因此您需要做的是添加1到結果值以計算偏移量,使其位於1..範圍內62

newch = substr(charset, ( (i+key)%62 )+1, 1)

答案2

substr()函數從字串中挑選出一個子字串。第二個參數給出的位置是從 1 開始計算的,而不是從 0 開始計算,而模數運算子在用作 時%62,會給出 0 到 61 之間的整數。

這意味著您必須修改您的substr()調用

newch = substr(charset, 1 + ((i + key)%62), 1)

或者,為了避免魔法常數,

newch = substr(charset, 1 + ((i + key)%length(charset)), 1)

相關內容