Shell脚本,使用awk进行对角单词搜索

时间:2021-01-15 01:06:24

Im looking for a way to get al the diagonal combinations from this block of letters:

我在寻找一种方法从这一大块字母中得到对角线组合:

a b c d e f 
h i j k l m 
o p q r s t 
v w x y z a 
c d e f g h 
j k l m n o 

i have this: awk '{++f; print $(f + 0)}' file.txt

我有这个:awk '{++f;打印$(f + 0)}' file.txt

but that only gets me ( if i can increase, f + 0, somehow with 1, 6times ):

但那只会得到(如果我可以增加f + 0,以某种方式增加1,6次)

a i q y g o
b j r z h
c k s a
d l t
e m 
f

and i need the other half to.. like this(does not have to be in this order:

而我需要另一半。像这样(不一定要按这个顺序排列:

a i q y g o
h p x f n
b j r z h
o w e m
c k s a
v d l
d l t
c k
e m
j 
f

3 个解决方案

#1


1  

perl -lane'
  push @r, [@F];
END {
  for my $n (0 .. $#r) {
    my (@x,@y);
    for (0 .. $#r) {
      push @x, $r[$n+$_][$_];
      push @y, $r[$_][$n+$_];
    }
    print "@x";
    print "@y" if $n;
  }
}
' file

output

输出

a i q y g o
h p x f n
b j r z h
o w e m
c k s a
v d l
d l t
c k
e m
j
f

#2


2  

You can use GNU datamash:

您可以使用GNU datamash:

$ datamash -t' ' transpose < file
a h o v c j
b i p w d k
c j q x e l
d k r y f m
e l s z g n
f m t a h o

#3


0  

This is a job for an array.

这是一个数组的任务。

#!/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 ""
    }
}

Here's a version of the above code that stores each diagonal word in an array.

这里是上述代码的一个版本,它将每个对角线词存储在一个数组中。

#!/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]
}

output

输出

aiqygo
hpxfn
bjrzh
owem
cksa
vdl
dlt
ck
em
j
f

Printing the orthogonal diagonals, eg 'flrxdj', is left as an exercise for the reader.

打印正交对角线(如“flrxdj”)是留给读者的练习。

...

Here's a function that prints all the subwords in a given word.

这是一个函数,它打印给定单词中的所有子单词。

#!/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") 
}

output

输出

Length: 1
A
B
C
D
Length: 2
AB
BC
CD
Length: 3
ABC
BCD
Length: 4
ABCD

#1


1  

perl -lane'
  push @r, [@F];
END {
  for my $n (0 .. $#r) {
    my (@x,@y);
    for (0 .. $#r) {
      push @x, $r[$n+$_][$_];
      push @y, $r[$_][$n+$_];
    }
    print "@x";
    print "@y" if $n;
  }
}
' file

output

输出

a i q y g o
h p x f n
b j r z h
o w e m
c k s a
v d l
d l t
c k
e m
j
f

#2


2  

You can use GNU datamash:

您可以使用GNU datamash:

$ datamash -t' ' transpose < file
a h o v c j
b i p w d k
c j q x e l
d k r y f m
e l s z g n
f m t a h o

#3


0  

This is a job for an array.

这是一个数组的任务。

#!/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 ""
    }
}

Here's a version of the above code that stores each diagonal word in an array.

这里是上述代码的一个版本,它将每个对角线词存储在一个数组中。

#!/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]
}

output

输出

aiqygo
hpxfn
bjrzh
owem
cksa
vdl
dlt
ck
em
j
f

Printing the orthogonal diagonals, eg 'flrxdj', is left as an exercise for the reader.

打印正交对角线(如“flrxdj”)是留给读者的练习。

...

Here's a function that prints all the subwords in a given word.

这是一个函数,它打印给定单词中的所有子单词。

#!/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") 
}

output

输出

Length: 1
A
B
C
D
Length: 2
AB
BC
CD
Length: 3
ABC
BCD
Length: 4
ABCD