Help needed in converting natural numbers to bit

Reply

Join Date: Dec 2007
Posts: 11
Reputation: ahjiefreak is an unknown quantity at this point 
Solved Threads: 1
ahjiefreak ahjiefreak is offline Offline
Newbie Poster

Help needed in converting natural numbers to bit

 
0
  #1
Mar 27th, 2008
Hi,

I have the following file input:-

Shell Scripting Syntax (Toggle Plain Text)
  1.  
  2.  
  3. 542 4795 61
  4. 543 4795 61
  5. 544 0 0
  6. 545 292 2
  7. 546 292 2
  8. 547 0 0
  9. 548 0 0
  10. 549 0 0
  11. 550 111 4
  12. 551 0 0
  13. 552 0 0
  14. 553 4614 63
  15. 554 4614 63
  16. 555 0 0
  17. etc...
  18.  
  19. The first column basically is an index. I would like to parse the second and third column to be bits.
  20.  
  21. In other words,
  22.  
  23. if I have
  24.  
  25. Index Col2 Col3
  26. 1 2 2
  27. 2 4 5
  28.  
  29. I wanted to change to bit wise to:-
  30.  
  31. #for first row
  32. 1 1
  33. 1 1
  34. #for second row
  35. 1 1
  36. 1 1
  37. 1 1
  38. 1 1
  39. 1 1
  40. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Help needed in converting natural numbers to bit

 
0
  #2
Mar 31st, 2008
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

Shell Scripting Syntax (Toggle Plain Text)
  1. # The scripts were written to be usefull in
  2. # a research enviornment, but anyone is welcome
  3. # to use them. Happy awking. -Tim Sherwood
  4.  
  5. func get01string(innum, t, retstr, i) {
  6.  
  7. retstr = "";
  8. t=innum;
  9. while( t )
  10. {
  11. if ( t%2==0 ) {
  12. retstr = "0" retstr;
  13. } else {
  14. retstr = "1" retstr;
  15. }
  16. t = int(t/2);
  17. }
  18.  
  19. return retstr;
  20. }
  21.  
  22.  
  23. {
  24. print get01string( $1 );
  25. }
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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC