简体   繁体   English

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

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

I have a script that I want to read a list of binary values and print them as follows (in columns, but not necessarily labelled accordingly)-我有一个脚本,我想读取二进制值列表并将它们打印如下(在列中,但不一定相应标记)-

binary decimal truncated truncated-decimal

etc.等等

-with subsequent columns being truncated sequentially by increasingly higher values. - 随后的列被越来越高的值顺序截断。 What I have so far only goes up to 3.到目前为止,我所拥有的最多只有 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

I've tried using column -t, column -x, paste, and join, but I keep getting errors.我尝试过使用 column -t、column -x、paste 和 join,但我不断收到错误。 I'd like to avoid outputting to a bunch of text files and then reading those - ideally keeping this self-contained.我想避免输出到一堆文本文件然后阅读它们——理想情况下保持这个独立。

I'm also wondering if anyone can tell me what the cleanest way is to simply ignore instances of the truncation value exceeding the bit count of the original value (ie truncating 100010101 by a value greater than '9', etc).我还想知道是否有人能告诉我最干净的方法是简单地忽略超过原始值位数的截断值实例(即,将 100010101 截断为大于“9”的值,等等)。 - that's what all the 2>/dev/nulls in there are (clumsily, I know) trying to avoid. - 这就是所有 2>/dev/null 都试图避免的(笨拙地,我知道)。

Here is the first 30 lines of the text file this is referencing and the only file (ideally) needed:这是引用的文本文件的前 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

The output file would be much longer, because it prints however many truncations are available based on the original numeric expansion which I would eventually want to go up to 21 (the largest bit weight in the file), but here are the first 30 lines again for comparison: output 文件会更长,因为它会打印,但是基于原始数字扩展可以使用许多截断,我最终希望 go 达到 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

Eventually I want to have a 5th column that won't be in the stdout but will be generated while the script runs based on factoring the values in the 4th column, and then grepping/awking such that no truncated values that aren't also prime will be printed:最终,我希望有一个第 5 列,它不会在标准输出中,但会在脚本运行时生成将打印:

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

Once greped+awked only these are prime:一旦 greped+awked 只有这些是素数:

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

Another problem I have is that I'm not sure how to make the two columns continue to print the same value while the other columns increment.我遇到的另一个问题是我不确定如何使两列继续打印相同的值,而其他列递增。

ie this:即:

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

And not this:而不是这个:

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

So the first 30 lines of the complete end result of what I eventually want to do is:所以我最终想要做的完整最终结果的前 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

The requirement seems to read (binary) numbers from a file, and for each value, generate multiple lines showing the decimal and binary representation of the (value, value/2, value/4, ..., value/(2^n))该要求似乎从文件中读取(二进制)数字,并为每个值生成多行显示 (value, value/2, value/4, ..., value/(2^n) 的十进制和二进制表示))

The solution is pure-bash, which means that it will perform well for relatively large files (no repeated call to 'bc' utility).解决方案是纯 bash,这意味着它对于相对较大的文件会表现良好(不会重复调用 'bc' 实用程序)。 The binary to decimal is using bash arithmetic expressions.二进制转十进制是使用 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM