简体   繁体   中英

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.

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. 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). - that's what all the 2>/dev/nulls in there are (clumsily, I know) trying to avoid.

Here is the first 30 lines of the text file this is referencing and the only file (ideally) needed:

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:

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:

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:

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:

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))

The solution is pure-bash, which means that it will perform well for relatively large files (no repeated call to 'bc' utility). The binary to decimal is using bash arithmetic expressions.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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