Operating System - HP-UX
1819803 Members
3172 Online
109607 Solutions
New Discussion юеВ

Re: Script needed to get the lines that has equal number of 1s and 0s

 
SOLVED
Go to solution
Karthik S S
Honored Contributor

Script needed to get the lines that has equal number of 1s and 0s

Hi,

The attached file contains binary representation of digits 0 to 1023.

ie:
0000000000
0000000001
...to.....
1111111111

Now I want to separate out only the lines that has equal number of 1s and 0s.

Ex:
0000011111
0101100011

Pl. help.

Thanks,
Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
20 REPLIES 20
Nicolas Dumeige
Esteemed Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

Hello Karthik

1) Did you forgot to attach the file ?
2) Wouldn't be enought to sum the "1" by lines.

Cheers

Nicolas
All different, all Unix
Karthik S S
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

I am sorry :-( .. forgot to attach the file. No, What I need is any line that contains equal number of 1s and 0s.

Thanks,
Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Karthik S S
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

If you are wondering why on this earth I am looking for such a script then,

From my earlier thread on FC-AL Addressing:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=549070

Qustion:
A private loop is typically run using a Fibre Channel hub. It can only address 126 devices because private loop uses an 8-bit address. But why only 126 devices? 2^8 = 256


Response:
The Fibre Channel uses an 8b/10b encoding (every 8 data bits are encoded in 10 'transmission bits' onto the wire), so there are potentially 4 different bit patterns to send a 'byte' over the cable. FC tries to make sure that the number of 0s and 1s are equally 'mixed'. This is done for self-clocking and maintaining the DC balance of the receiver. For error detection the receiver counts the number of 0s and 1s to calculate the 'running disparity'. FC_AL requires a negative value at the end of a 'sub-block'.

For some 8-bit values there exist 10-bit pattern that contain strings of zeros or ones that are longer than 5. So, in the end there are only (if I recall correctly) 138 usable 8-bit values, but some of them are reserved to indicate special 'functions' (e.g. fair arbitration on the FC_AL) and the remaining 126 are usable for devices.

From Some Doc on FC Basics:
Disparity
The 8-bit and 10-bit encoded bytes have a property known as disparity. The loop ID and the AL_PA disparity properties can be positive, negative, or neutral. An 8-bit and 10-bit byte has negative disparity if there are more binary ones in the byte than binary zeroes. Conversely, the byte has positive disparity if there are more binary zeroes than ones.

Neutral disparity is when the number of binary ones equals the number of binary zeroes. This is required to eliminate clocking errors between sender and receiver.
---------------------------------

So I just wanted to make sure that there are really only 134 addresses which have the neutral disparity in a 8 bit address space with 8/b encoding.

Thanks,
Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Francisco J. Soler
Honored Contributor
Solution

Re: Script needed to get the lines that has equal number of 1s and 0s

Hi Karthik,

You can do it with awk and the gsub function:

--- s.awk -----
{
line=$0
n_one=gsub("1","x")
n_zero=gsub("0","x")
if (n_one==n_zero)
print(line)
}
---------------

Now make:

awk -f s.awk 100868.null


Hope this helps.

Frank.
Linux?. Yes, of course.
Fred Ruffet
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

a perl one :

script.pl
--------
while () {
chomp;
$line=$_;
$line0=$line;
$line1=$line;
$line0=~ s/[^0]//g;
$line1=~ s/[^1]//g;
if (length($line0) == length($line1)) {
print "$line\n";
}
}
--------

then run
perl script.pl < yourfile

regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Karthik S S
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

Thanks guys. Really great scripts ..!!

But still I am confused with the FC-AL Addressing. I am still not convinced why do we get only 126 usable addresses out of 256.

Anyways I will post it back to Storage forums.

Thanks,
Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Karthik S S
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

With 8 bit addressing (8/10b encoding) I get 256 addresses (check the attached file). Out of them only 55 are having neutral disparity (using the scripts I separated out the lines that has equal number of 1s and 0s). That still leaves 256 - 55 = 201 addresses.

Thanks,
Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Stefan Farrelly
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

Heres a script to do it - who needs awk or perl!!:-)

for i in $(cat t)
do
let x=$(echo $i|wc -c)-1
let z=0
let ones=0
let zeroes=0
while [ $z -lt $x ]
do
let z=$z+1
char=$(echo $i|cut -b $z)
[ $char = 1 ] && let ones=$ones+1
[ $char = 0 ] && let zeroes=$zeroes+1
done
echo $i $ones $zeroes
[ $ones = $zeroes ] && echo $i has same number of 1's and 0's
done

Input file needs to be called t as in line 1 (cat t)
Im from Palmerston North, New Zealand, but somehow ended up in London...
Karthik S S
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

Stefan,

Your script works great too ..!! But little slow though as expected.

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Karthik S S
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

I thought this site will be useful for the members:

BINARY TABLE GENERATOR
http://www.subterrane.com/bintab.shtml

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Elmar P. Kolkman
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

A simpler solution would be:

cat file | while read line
do
if [ $(echo $line | sed 's|0||g') = "1111" ]
then
echo $line
fi
done

Same can be done using awk and perl, of course.
Every problem has at least one solution. Only some solutions are harder to find.
Hein van den Heuvel
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s


I had a perl script that answerred a similar question hanging arround. I added a line or two for you. A little overkill but here is a sample output:

perl bits.pl 3
0 000 000 zero=3, one=0, longest=3 (0)
1 001 001 zero=2, one=1, longest=2 (0)
2 002 010 zero=2, one=1, longest=1 (0)
3 003 011 zero=1, one=2, longest=2 (1)
4 004 100 zero=2, one=1, longest=2 (0)
5 005 101 zero=1, one=2, longest=1 (1)
6 006 110 zero=1, one=2, longest=2 (1)
7 007 111 zero=0, one=3, longest=3 (1)

and:
perl bits.pl 8 | grep "e=4" | sort -k 6 | tail -4
15 00F 00001111 zero=4, one=4, longest=4 (1)
30 01E 00011110 zero=4, one=4, longest=4 (1)
60 03C 00111100 zero=4, one=4, longest=4 (1)
120 078 01111000 zero=4, one=4, longest=4 (1)


Script below.
Enjoy...

Hein.

---- bits.pl ---

$w = shift or die "How many bits wide?";
while ($i < (1<<$w)) {
$zero = $one = $long = $longest = $n = 0;
$last = 2;
$tmp = $i;
while ($n++ < $w) {
$bit = $tmp & 1;
$tmp >>= 1;
if ($bit) { $one++ } else { $zero++ }
$long++ if ($bit == $last);
if ($long > $longest) { $longest = $long; $long_last = $last};
if ($bit != $last) { $last = $bit; $long = 1 };
# print "bit=$bit, last=$last, zero=$zero, one=$one, longest=$longest, ll=$long_last\n";
}
printf ("%4d %03X %.*b zero=%d, one=%d, longest=%d (%d)\n",
$i,$i,$w,$i,$zero,$one,$longest,$long_last);
$i++;
}
Bharat Katkar
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

Karthik, for this statement:

"But still I am confused with the FC-AL Addressing. I am still not convinced why do we get only 126 usable addresses out of 256."

If all 256 possible 8-bit bytes are dispatched to the 8b/10b encoder only 134 imerge with neutral disparity. Out of this 7 are used by Fibre Channel for special purpose and what remains is 127 and only that can be used as ALPA because ALPA requires neutral disparity. Again 1 goes for FL_port and 126 remains for NL_ports.

For more details refer:
:Designing Storage Area Networks:
By Tom Clark
Chapter 4. Page 53/54




You need to know a lot to actually know how little you know
Karthik S S
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

Hi Bharat,

But do you have a table that maps 8 bit characters to a 10 bit encoded characters?? I want understand how they calculate 8/10b encoding.

Thanks,
Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Bharat Katkar
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s


Basically 8b/10b coding was used to cope with high speed (Gigabit) transmission. This is because hex FF (1111 1111) would appear as a sustained DC positive voltage on the line and one cant differentiate between two bits of data.
SO every 8 bit byte that is transmitted will go thr' this encoder so that it is possible to detect when one bit ended and another began.

8b/10b was first developed by IBM and what it does is converts each 8 bit byte into two possible 10 bit characters, one with positive disparity and other with negative. So encoder decides which parity to used depending on the one used for earlier 8 bit byte.It could be positive or negative. If positive is used negative is discarded. This concept is bit confusing but if you know how Arbitration in FC-AL loop works then it is simple to understand.

This encoders are nothing but Firmware sitting on HBA in your Machine. You can compare them with Transciever on NIC.

Made an effort to clear this a bit. Actually this is very big topic and diffcult to mention all related things.

Hope this clear your doubt.

Regards,
Bharat

You need to know a lot to actually know how little you know
Uwe Zessin
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

Huh?
I don't think the arbitration process has anything to do with disparity.
.
Bharat Katkar
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

Okay, not directly but once you know arbitration, you know following things:

1. What is ALPA?
2. Primitive Signals like ARB(x), IDLE, ARB(F0), etc.
3. How to differentiate between this Signals?

Along with this it is also important to know loop addressing and loop initialization process.

THis help you understand why such kind of addressing, what primitive signals are and how actual data is diffentiated from control signals.

Uwe, please correct me if am wrong with small desription.
Thanks
You need to know a lot to actually know how little you know
Uwe Zessin
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

You description looks almost fine to me - I am not in the mood for nit-picking right now.

Arbitration itself does not care how the data is encoded on the wire. You can also do it on a piece of paper or a board at the wall.

The problem is that FC_AL was tacked on later, so it has to live with some limitations (e.g. not every data byte has a transmission character with neutral disparity or one that can force a negative disparity) - see my response from today at:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=549070

I agree that it is nice to understand why, e.g. 3 cannot be used as an AL_PA, but it is not required for an end-user.


With all those questions it looks like Karthik wants to build his own Fibre Channel equipment ;-)
.
Karthik S S
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

Thanks Uwe & Bharat.

Uwe, I am not joining this game :-)) .. I have no plans of becoming a Fiber Scientist ;-).

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Uwe Zessin
Honored Contributor

Re: Script needed to get the lines that has equal number of 1s and 0s

And I was suspecting you are preparing to work on the future 100 GBit ;-)
.