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 index. I would like to parse the second and third column to be bits.

In other words,

if I have

Index Col2 Col3
1        2       2
2        4       5

I wanted to change to bit wise to:-

#for first row
1 1
1 1
#for second row
1 1
1 1
1 1
1 1
1 1
0 1

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.For your information, i am using a bash/sh script for this.

Please advise. Thanks alot.

Hey There,

All credit to the guy who wrote this function to convert decimal to binary (included in the snippet below), but you could use awk to do this and apply this function to the 2nd and 3rd fields in each line/record ($2 $3)

Hope this helps - let me know if you have any difficulty incorporating it into what you've already got going and, if possible, post what you have and let me know where you're having the issue.

Best wishes :)

, Mike

# The scripts were written to be usefull in
# a research enviornment, but anyone is welcome
# to use them.  Happy awking.  -Tim Sherwood

func get01string(innum,       t, retstr, i) {

        retstr = "";
        t=innum;
	while( t )
        {
                if ( t%2==0 ) {
                        retstr = "0" retstr;
                } else {
                        retstr = "1" retstr;
                }
                t = int(t/2);
        }

        return retstr;
}


{
	print	get01string( $1 );
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.