這是一個數組的工作。
#!/usr/bin/awk -f
# Print diagonals from a square of text.
# This script has no error checking.
# Written by PM 2Ring 2014.10.13
{
for(i=1; i<=NF; i++)
a[NR,i] = $i
}
END{
for(i=1; i<=NR; i++)
{
for(j=1; i+j-1<=NR; j++)
printf "%s ", a[i+j-1, j]
print ""
if (i == 1)
continue
for(j=1; i+j-1<=NR; j++)
printf "%s ", a[j, i+j-1]
print ""
}
}
下面是存儲在陣列中的每個對角字上面的代碼的一個版本。
#!/usr/bin/awk -f
# Print diagonals from a square of text.
# This script has no error checking.
# Written by PM 2Ring 2014.10.13
{
for(i=1; i<=NF; i++)
a[NR,i] = $i
}
END{
k = 1
for(i=1; i<=NR; i++)
{
word = ""
for(j=1; i+j-1<=NR; j++)
word = word a[i+j-1, j]
words[k++] = word
if (i == 1)
continue
word = ""
for(j=1; i+j-1<=NR; j++)
word = word a[j, i+j-1]
words[k++] = word
}
numwords = k
for (k=1; k<numwords; k++)
print words[k]
}
輸出
aiqygo
hpxfn
bjrzh
owem
cksa
vdl
dlt
ck
em
j
f
打印正交對角線,例如 'flrxdj',留給讀者作爲練習留給讀者。
...
下面是打印在一個給定的單詞的所有子詞的功能。
#!/usr/bin/awk -f
# Print subwords of a given word
# Written by PM 2Ring 2014.10.13
function print_subwords(s, i, j, len)
{
len = length(s)
for (i=1; i<=len; i++)
{
print "Length:", i
for (j=1; i+j-1 <= len; j++)
print substr(s, j, i)
}
}
BEGIN{
print_subwords("ABCD")
}
輸出
Length: 1
A
B
C
D
Length: 2
AB
BC
CD
Length: 3
ABC
BCD
Length: 4
ABCD
那麼,什麼是全所需的輸出? – 2014-10-11 17:41:03
更新了我的問題 – Brutalized 2014-10-11 18:05:33