Help needed in converting number to bit

Hi,

I have the following file input:-

542 4795 61
543 4795 61
544 0 0
545 292 2
546 292 2
547 0 0
548 0 0
549 0 0
550 111 4
551 0 0
552 0 0
553 4614 63
554 4614 63
555 0 0
etc...

The first column basically is an known index. I would like to parse the second and third column to be bits.

In other words,

if I have

Index
1 2 2
2 4 5

I wanted to change to bit wise to:-

1 1
1 1

1 1
1 1
1 1
1 1
1 1
0 1
etc..

Can anyone help me on this. I tried using C and it works but I need to assign "while loop" for each index for the tokenization.IF that is the case, if I have thousands of indexes, i would have to have thousands of while.

Please advise. Thanks alot.

It's not terribly obvious how the mapping in your example goes. In fact, there is no conversion to bits going on, the number just says how many ones to print, and the numbers on one input line are to be printed out next to each other until the number of "1" lines indicated by the biggest number has been printed? That's the only interpretation I can come up with for this.

perl -ane '
shift @F;  # get rid of index
$have = 1;
while (1) {
  $sep = "";
  @a = ();
  for ($i = 0; $i < @F; ++$i) {
    $have = 0;
    if ($F[$i]-- > 0) {
      push @a, 1;
      $have = 1;
    } else {
      push @a, 0;
    }
  }
  if ($have) {
    print (join (" ", @a), "\n")
  } else {
    last;
  }
}
print "\n" '

Hi,

Apologies if there is any confusion.

Index Col2 Col3
1 2 2
2 4 5

(The heading i just added for illustration)

I wanted to change the Col2 and Col3 to bit wise to:-

From "2", it will change to bit twice..if the value is "3", it will basically print out 1s for three times. The same goes for Column 3.

E.g if col2 value for index 2 is 4, it will print 4 times of 1 toghether with Col3 except Col3 will have extra line.

1 1
1 1
1 1
1 1
1 1
0 1

I am currently using bash/sh script. Please advise. Thanks again.

And you did not take the time to check if the script I posted would do exactly what you described?

Hi,

I have read the code you posted earlier. It seems that it only pushes the non zero and zeros for the first column for all the index rows and then print out newline hence exit.

And I am not too sure on how to use Perl in this case which is why I was hoping to have an example using bash.

Thanks.

As far as I can tell, the code matches the spec you gave. The example input generates the desired example output. If there are nuances which are missing, please elaborate on what they are.

vnix$ perl /tmp/nuisance <<HERE
> 1 2 2
> 2 4 5
> HERE
1 1
1 1

1 1
1 1
1 1
1 1
0 1

vnix$