-1

I have similar output like below goes so long. I want to create columns with this output but with like groups/blocks. And process output after a pipe.

id: c2b751c227111edfgdghfhfg19079a466e1916f6df4
blob_size: 1965
weight: 1965
fee: 0.000015690000
fee/byte: 0.000000007984
receive_time: 1613453355 (3 minutes ago)
relayed: 16134535355 (3 minutes ago)
do_not_relay: F
kept_by_block: F
double_spend_seen: F
max_used_block_height: 2279513
max_used_block_id: 44fbf6656dff890aedc29ashfhdfh2f3f848dd94c2dcb79562f5b
last_failed_height: 0
last_failed_id: 0000000000000000000000000000000000000000000000000000000000000000

id: be7e8ec30c8fff9deb33702fd392a566e4a44192138086aaff2d0c9fa82cdbfe
blob_size: 1968
weight: 1968
fee: 0.000015710000
fee/byte: 0.000000007982
receive_time: 13452 (36 seconds ago)
relayed: 16113434534512 (36 seconds ago)
do_not_relay: F
kept_by_block: F
double_spend_seen: F
max_used_block_height: 2234534515
max_used_block_id: 16433ec85ab8a9e81ac714c2cc3171149dfgdfgf34eec4d42e81
last_failed_height: 0
last_failed_id: 0000000000000000000000000000000000000000000000000000000000000000

Something like;

id: 234234                id: 234234234
blob_size: 234234         blob_size: 2342342
.                         .
.                         .
.                         .
[empty line]              [empty line]
id :234234234             id: fwfsdfsdfsdf
blob_size: 24234234       blob_size: 234234
.                         .
.                         .
.                         .

I hope someone can help.

Cheers

4
  • 1
    How do you decide where to break rows and columns? Are you filling the first row/column to a maximum number of entries and then breaking to the next? Or is there some other logic?
    – bu5hman
    Commented Jan 23, 2021 at 8:27
  • may be break at empty line or what starts with "id:". Every row is 2 columns. and doesnt matter first rows or columns are gonna filled.
    – Yanik
    Commented Jan 23, 2021 at 10:49
  • 1
    Your example output doesn't match your example input. It's not clear, for example, whether you're going down and across, or across and down. Commented Jan 23, 2021 at 18:05
  • Sample input with more blocks but fewer lines per block and shorter lines would have been better to demonstrate your problem and easier to test with. Putting ...s in your input/output is far less useful than actual values.
    – Ed Morton
    Commented Jan 24, 2021 at 16:28

3 Answers 3

1

I added a 3rd block to your sample input to show that this can handle an odd number of blocks without repeating or dropping one and I made each line no more than 20 chars wide and reduced how many lines per block there are (see the bottom of this answer) so we can easily see the way the output is being separated into rows of columns:

$ cat tst.awk
BEGIN {
    RS = ""
    FS = "\n"
    OFS = "\t"
    maxCols = (maxCols ? maxCols : 2)
}
{
    numRows = NF
    numCols++
    for (rowNr=1; rowNr<=numRows; rowNr++) {
        vals[rowNr,numCols] = $rowNr
    }
}
(NR%maxCols) == 0 { prt() }
END { prt() }

function prt(   rowNr,colNr) {
    if ( numCols != 0 ) {
        for ( rowNr=1; rowNr<=numRows; rowNr++) {
            for (colNr=1; colNr<=numCols; colNr++) {
                printf "%s%s", vals[rowNr,colNr], (colNr<numCols ? OFS : ORS)
            }
        }
        print ""
        numCols = 0
    }
}

$ awk -f tst.awk file | column -s$'\t' -tL
id: c2b751c227111edf  id: be7e8ec30c8fff9d
blob_size: 1965       blob_size: 1968
weight: 1965          weight: 1968
fee: 0.000015690000   fee: 0.000015710000
fee/byte: 0.00000000  fee/byte: 0.00000000

id: blahblahblahblah
blob_size: 1968
weight: 1968
fee: 0.000015710000
fee/byte: 0.00000000

If you want the number of blocks (columns) per row output to be something other than 2, just set maxCols to however many blocks per row you want output:

$ awk -v maxCols=3 -f tst.awk file | column -s$'\t' -tL
id: c2b751c227111edf  id: be7e8ec30c8fff9d  id: blahblahblahblah
blob_size: 1965       blob_size: 1968       blob_size: 1968
weight: 1965          weight: 1968          weight: 1968
fee: 0.000015690000   fee: 0.000015710000   fee: 0.000015710000
fee/byte: 0.00000000  fee/byte: 0.00000000  fee/byte: 0.00000000

The above was run against this input file:

$ cat file
id: c2b751c227111edf
blob_size: 1965
weight: 1965
fee: 0.000015690000
fee/byte: 0.00000000

id: be7e8ec30c8fff9d
blob_size: 1968
weight: 1968
fee: 0.000015710000
fee/byte: 0.00000000

id: blahblahblahblah
blob_size: 1968
weight: 1968
fee: 0.000015710000
fee/byte: 0.00000000
0

start with;

| perl -pe 's/\n/,/g' \
| perl -pe 's/,(id:)/\n$1/g' \
| csvtool transpose - \
| column -t -s ,

Then break into 2 columns with a loop (and optional trimming);

TC=""
while read L ; do
    if [ "$TC" == "" ] ; then
            TC="$L"
    else
            echo -e "$TC\n$L" \
            | csvtool transpose - \
            | column -t -s ,
            echo
            TC=""
    fi
done < <(cat test.txt \
| perl -pe 's/^(.{24}).*/$1.../g;
s/\n/,/g' \
| perl -pe 's/,(id:)|$/\n$1/g')
echo "$TC" \
| csvtool transpose -
3
  • Thanks for reply. But it create columns side by side with every block. what i need is 2 columns. and rows goes down.
    – Yanik
    Commented Jan 23, 2021 at 10:45
  • Thanks a lot. Working perfectly..
    – Yanik
    Commented Jan 23, 2021 at 12:38
  • Marked as answer but "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score."
    – Yanik
    Commented Jan 23, 2021 at 17:11
0

The horse has bolted, but if the number of lines per record is fixed at 14 plus a blank separating line then need it be any more complex than finding the mid point?

l=$(cat file | wc -l); s=$(( (l / 30)*15+14 )); t=$(( l-s-1 )); 
  paste <(head -n $s file) <(tail -n $t file)
1
  • Never name a variable l as it looks far too much like the number 1, indistinguishable in some fonts, and so obfuscates your code.
    – Ed Morton
    Commented Jan 24, 2021 at 17:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .