| | |
Help needed in converting natural numbers to bit
![]() |
•
•
Join Date: Dec 2007
Posts: 11
Reputation:
Solved Threads: 1
Hi,
I have the following file input:-
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.
I have the following file input:-
Shell Scripting Syntax (Toggle Plain Text)
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.
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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
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
Shell Scripting Syntax (Toggle Plain Text)
# 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 ); }
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
![]() |
Other Threads in the Shell Scripting Forum
- Previous Thread: Splitting the line in multiple lines
- Next Thread: Issues emailing data from unix/sql script
| Thread Tools | Search this Thread |





