Help needed in converting number to bit


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed in converting number to bit
# 1  
Old 03-26-2008
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.
# 2  
Old 03-27-2008
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.

Code:
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" '

# 3  
Old 03-27-2008
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.
# 4  
Old 03-27-2008
And you did not take the time to check if the script I posted would do exactly what you described?
# 5  
Old 03-27-2008
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.
# 6  
Old 03-30-2008
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.

Code:
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$

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Converting negative number to positive in a file

Hi ALL, I am having semi column separated file as below. I am having negative values for the records starting with 11095. How can I convert that positive number I tried this below seems not working sed 's/ \(*\)$/ -\1/;t;s/\(.*\)-/\1/ myfile myfile... (6 Replies)
Discussion started by: arunkumar_mca
6 Replies

2. Programming

Number to bit vector

Is there a function to convert number (unsigned int for this example) to binary? I could not find a simple one thru google. While I was learning bloom filter with the example, I was wondering if anybody can help me to 1) display the real bits vector for the bloomfilter; 2) if dataset is very... (11 Replies)
Discussion started by: yifangt
11 Replies

3. Programming

How bit representation of a number can be done in c?

I have an assignment in which a character is the input of which some bits(from a position to certain position) are to be inverted (1's complement) and then the resultant character is to be returned....for example unsigned char x = J from p = 3 to offset n = 5 01001010 inverted to... (1 Reply)
Discussion started by: ezee
1 Replies

4. UNIX for Dummies Questions & Answers

converting string to number in shell script

Hi, I am having a problem in converting a string to number so I can preform arithmetic operations. timeTot=0 timeTmp=$(cat idsOutput | grep 'Run time' | cut -c 36-39) timeTot=$ #This is line 28 echo "total RunTime=" $timeTot this is the error msg: ./ids2.sh: line 28: 0+1.35: syntax... (8 Replies)
Discussion started by: turki_00
8 Replies

5. UNIX for Dummies Questions & Answers

Installation help needed(64 bit or 32 bit)

Hi , I have to install Oracle Enterprise linux 64bit version on my machine. How would i determine whether my system is compatible for 64 bit version. What is difference between 64 bit version and 32 bit version? I heard that 64 bit version are suitable for machine with ram more than 4 gb... (5 Replies)
Discussion started by: pinga123
5 Replies

6. HP-UX

Tru 64 bit on HP AlphaServer ES45 creating problem Help Needed !!!

Dear All, I am new on Tru 64 bit Unix platform. I have a database server "HP AlphaServer ES45" version p5.1 of Tru Ux 64 bit installed. I have 4 hard drives in this system (1,2,3,4). My 4th hard drive is damaged which contain Oracle database files including partitions on 4th drives... (2 Replies)
Discussion started by: hellozishan
2 Replies

7. Shell Programming and Scripting

Problem in converting number in shell script

Hi All, I am writing a shell script in which I want to convert a number like : Suppose the number is "98487657" and we have to convert it to "98000000", what I want to do is to retain first 2 digits and convert all remaining digits to "0". Number could be of any length (length... (4 Replies)
Discussion started by: amitanshu.verma
4 Replies

8. Shell Programming and Scripting

Converting Stirngs to number

I have written a script to compare the values of string (which actually are numbers) but it is not giving the expected output. Please suggest for the script. #!/usr/bin/ksh sysdate=20071009 echo $sysdate currfile=20071008 echo $currfile if ; then echo "Equal" else echo "Not... (2 Replies)
Discussion started by: raman1605
2 Replies

9. Shell Programming and Scripting

need help converting string to number for calculation

I've searched the forum and google, but can't see an answer to this simple problem. Here's my small test script: #!/bin/csh echo "enter a number:" read num echo "you entered $num" set num = `expr $num + 1` echo new value is $num can someone show me how to do this calculation? note that... (4 Replies)
Discussion started by: tpatput
4 Replies
Login or Register to Ask a Question