943,682 Members | Top Members by Rank

Ad:
Mar 27th, 2008
0

Help needed in converting natural numbers to bit

Expand Post »
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ahjiefreak is offline Offline
11 posts
since Dec 2007
Mar 31st, 2008
0

Re: Help needed in converting natural numbers to bit

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. }
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Splitting the line in multiple lines
Next Thread in Shell Scripting Forum Timeline: Issues emailing data from unix/sql script





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC