如何将多个单独的 for 循环输出打印为 bash 中的列

how to print multiple separate for loop outputs as columns in bash

我有一个脚本,我想读取二进制值列表并按如下方式打印它们(在列中,但不一定相应地标记)-

binary decimal truncated truncated-decimal

等等

-后续列被越来越高的值顺序截断。到目前为止我只有 3 个。

for z in {1..3}; 
    do
    p2=$(for n2 in $(cat binary_primes.txt ); 
            do echo $n2; 
        done)                                   #prints original values
    p10=$(for n10 in $(echo $p2); 
            do echo "ibase=2;$n10;obase=10" | bc; 
        done)                                   #prints original values in decimal
    a2=$(for y in $(cat binary_primes.txt ); 
            do 
            xx=${y:0:${#y}-$z} 2>/dev/null; echo 
            $xx;                                
        done)                                   #original value truncated by 1 bit
    a10=$(for y2 in $(echo $a2); 
            do echo "ibase=2;$y2;obase=10" | bc; 
        done)                                   #the latter in decimal
    b2=$(for x in $(cat binary_primes.txt ); 
            do xx=${x:0:${#x}-$z} 2>/dev/null; 
            echo $xx; 
        done)                                   #original value truncated by 2 bits
    b10=$(for x2 in $(echo $b2); 
            do echo "ibase=2;$x2;obase=10" | bc; 
        done)                                   #the latter in decimal
    c2=$(for w in $(cat binary_primes.txt ); 
            do xx=${w:0:${#w}-$z} 2>/dev/null; 
            echo $xx; 
        done)                                   #original value truncated by 3 bits
    c10=$(for w2 in $(echo $c2); 
            do echo "ibase=2;$w2;obase=10" | bc; 
        done)                                   #the latter in decimal
    echo $p2 $p10 $a2 $a10 $b2 $b10 $c2 $c10;
done

我试过使用 column -t、column -x、paste 和 join,但我总是收到错误。我想避免输出到一堆文本文件然后再阅读它们 - 最好保持这种独立。

我也想知道是否有人能告诉我最干净的方法是简单地忽略超过原始值位数的截断值的实例(即用大于“9”的值截断 100010101,等等) ). - 这就是所有 2>/dev/nulls 都在(笨拙地,我知道)试图避免的。

这是引用的文本文件的前 30 行,也是唯一(理想情况下)需要的文件:

100111101010011111101
100111101010011101001
100111101010011011001
100111101010011000101
100111101010010111111
100111101010010110101
100111101010010101111
100111101010010010001
100111101010001111111
100111101010001100001
100111101010001010101
100111101010001001101
100111101010000101011
100111101010000100011
100111101010000000001
100111101001111111011
100111101001111111001
100111101001111101111
100111101001111101101
100111101001110110011
100111101001110110001
100111101001110100111
100111101001110011111
100111101001110010101
100111101001110001111
100111101001110001101
100111101001101111011
100111101001101110101
100111101001101100011
100111101001101011001

输出文件会更长,因为它打印了很多截断,但基于原始数字扩展,我最终希望达到 21(文件中最大的位权重),但这里是再次比较前30行:

100111101010011111101  1299709  100111101010011111101  1299709
100111101010011111101  1299709  10011110101001111110   649854
100111101010011111101  1299709  1001111010100111111    324927
100111101010011111101  1299709  100111101010011111     162463
100111101010011111101  1299709  10011110101001111      81231
100111101010011111101  1299709  1001111010100111       40615
100111101010011111101  1299709  100111101010011        20307
100111101010011111101  1299709  10011110101001         10153
100111101010011111101  1299709  1001111010100          5076
100111101010011111101  1299709  100111101010           2538
100111101010011111101  1299709  10011110101            1269
100111101010011111101  1299709  1001111010             634
100111101010011111101  1299709  100111101              317
100111101010011111101  1299709  10011110               158
100111101010011111101  1299709  1001111                79
100111101010011111101  1299709  100111                 39
100111101010011111101  1299709  10011                  19
100111101010011111101  1299709  1001                   9
100111101010011111101  1299709  100                    4
100111101010011111101  1299709  10                     2
100111101010011111101  1299709  1                      1
100111101010011101001  1299689  100111101010011101001  1299689
100111101010011101001  1299689  10011110101001110100   649844
100111101010011101001  1299689  1001111010100111010    324922
100111101010011101001  1299689  100111101010011101     162461
100111101010011101001  1299689  10011110101001110      81230
100111101010011101001  1299689  1001111010100111       40615
100111101010011101001  1299689  100111101010011        20307
100111101010011101001  1299689  10011110101001         10153
100111101010011101001  1299689  1001111010100          5076

最终我想要第 5 列,它不会出现在标准输出中,但会在脚本运行时根据第 4 列中的值进行分解生成,然后 grepping/awking 这样就不会被截断将打印不是质数的值:

1299709: 1299709
649854: 2 3 3 79 457
324927: 3 3 79 457
162463: 7 23209
81231: 3 27077
40615: 5 8123
20307: 3 7 967
10153: 11 13 71
5076: 2 2 3 3 3 47
2538: 2 3 3 3 47
1269: 3 3 3 47
634: 2 317
317: 317
158: 2 79
79: 79
39: 3 13
19: 19
9: 3 3
4: 2 2
2: 2
1:
1299689: 1299689
649844: 2 2 13 12497
324922: 2 13 12497
162461: 13 12497
81230: 2 5 8123
40615: 5 8123
20307: 3 7 967
10153: 11 13 71
5076: 2 2 3 3 3 47

一旦 greped+awked 只有这些是质数:

1299709: 1299709
317: 317
79: 79
19: 19
2: 2
1299689: 1299689

我遇到的另一个问题是我不确定如何让两列继续打印相同的值,而其他列递增。

即这个:

100111101010011111101  1299709  100111101010011111101  1299709
100111101010011111101  1299709  100111101              317
100111101010011111101  1299709  1001111                79
100111101010011111101  1299709  10011                  19
100111101010011111101  1299709  10                     2

而不是这个:

100111101010011111101  1299709  100111101010011111101  1299709
100111101010000101011  1299499  100111101              317
100111101010000000001  1299457  1001111                79
100111101001111111001  1299449  10011                  19
100111101001110110011  1299379  10                     2

所以我最终想要做的完整最终结果的前 30 行是:

100111101010011111101  1299709  100111101010011111101  1299709
100111101010011111101  1299709  100111101              317
100111101010011111101  1299709  1001111                79
100111101010011111101  1299709  10011                  19
100111101010011111101  1299709  10                     2
100111101010011101001  1299689  100111101010011101001  1299689
100111101010011101001  1299689  100111101              317
100111101010011101001  1299689  1001111                79
100111101010011101001  1299689  10011                  19
100111101010011101001  1299689  10                     2
100111101010011011001  1299673  100111101010011011001  1299673
100111101010011011001  1299673  100111101              317
100111101010011011001  1299673  1001111                79
100111101010011011001  1299673  10011                  19
100111101010011011001  1299673  10                     2
100111101010011000101  1299653  100111101010011000101  1299653
100111101010011000101  1299653  100111101              317
100111101010011000101  1299653  1001111                79
100111101010011000101  1299653  10011                  19
100111101010011000101  1299653  10                     2
100111101010010111111  1299647  100111101010010111111  1299647
100111101010010111111  1299647  100111101              317
100111101010010111111  1299647  1001111                79
100111101010010111111  1299647  10011                  19
100111101010010111111  1299647  10                     2
100111101010010110101  1299637  100111101010010110101  1299637
100111101010010110101  1299637  100111101              317
100111101010010110101  1299637  1001111                79
100111101010010110101  1299637  10011                  19
100111101010010110101  1299637  10                     2

要求似乎是从文件中读取(二进制)数字,并为每个值生成多行,显示(值,value/2,value/4,。 .., 值/(2^n))

解决方案是pure-bash,这意味着它将对相对较大的文件执行良好(无需重复调用'bc' 实用程序)。二进制转十进制是用bash算术表达式。

#! /bin/bash

while read b ; do
    # Convert binary 'b' to decimal 'd'
    d=$((2#$b))
    bb=$b
    dd=$d
    b_len=${#b}
    while (( dd > 0 )) ; do
        printf "%s %d %-${b_len}s %d\n" $b $d $bb $dd
        dd=$((dd/2))
        bb=${bb:0:${#bb}-1}
    done
    
done < binary_primes.txt