looking for the best way to compare two system.objects. tried the compare-object option, doesn't seem to give the desired output..
> $abc
BGP summary information for VRF default
Router identifier 192.168.0.3, local AS number 7251
Neighbor Status Codes: m - Under maintenance
Neighbor V AS MsgRcvd MsgSent InQ OutQ Up/Down State PfxRcd PfxAcc
10.12.103.119 4 7251 0 0 0 0 5d23h Connect
10.46.252.121 4 7251 0 0 0 0 5d23h Connect
> $def
BGP summary information for VRF default
Router identifier 192.168.0.3, local AS number 7251
Neighbor Status Codes: m - Under maintenance
Neighbor V AS MsgRcvd MsgSent InQ OutQ Up/Down State PfxRcd PfxAcc
10.12.103.119 4 7251 0 0 0 0 5d23h Active
10.46.252.121 4 7251 0 0 0 0 5d23h Estab
> $abc.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
> $def.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
> Compare-Object -ReferenceObject $abc -DifferenceObject $def
InputObject
-----------
BGP summary information for VRF default...
BGP summary information for VRF default...
You're dealing with strings ([System.String]), not [System.Objects] - the latter is only the base type, i.e., what [System.String] derives from.
The best way to compare 2 multiline strings is to compare them line by line, and to that end you must split them into an array of lines:
PS> Compare-Object ($abc -split '\r?\n') ($def -split '\r?\n')
InputObject SideIndicator
----------- -------------
10.12.103.119 4 7251 0 0 0 0 5d23h Active =>
10.46.252.121 4 7251 0 0 0 0 5d23h Estab =>
10.12.103.119 4 7251 0 0 0 0 5d23h Connect <=
10.46.252.121 4 7251 0 0 0 0 5d23h Connect <=
The => lines (the .SideIndicator property value of the output objects representing differences) indicate lines exclusive to the RHS ($def) whereas <= indicates lines exclusive to the LHS ($abc)
Related
I have the flag column which has 0 and 1 values.
I want to count the 1s between two 0s
flag
0
0
1
1
0
1
1
1
Expected output
0
0
1
2
0
1
2
3
Is it possible to tell whether a process/thread has the PF_NO_SETAFFINITY flag set? I'm running taskset on a series of process ids and some are throwing errors of the following form:
taskset: failed to set pid 30's affinity: Invalid argument
I believe this is because some processes have PF_NO_SETAFFINITY set (see Answer).
Thank you!
Yes - look at /proc/PID/stat's 'flag' field
<linux/sched.h
#define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_allowed */
Look here for details on using /proc:
http://man7.org/linux/man-pages/man5/proc.5.html
https://supportcenter.checkpoint.com/supportcenter/portal?eventSubmit_doGoviewsolutiondetails=&solutionid=sk65143
Example:
ps -eaf
www-data 30084 19962 0 07:09 ? 00:00:00 /usr/sbin/apache2 -k start
...
cat /proc/30084/stat
30084 (apache2) S 19962 19962 19962 0 -1 4194624 554 0 3 0 0 0 0 0 20 0 1 0 298837672 509616128 5510 18446744073709551615 1 1 0 0 0 0 0 16781312 201346799 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0
The flags are 4194624
Q: Do you mind specifying how you'd write a simple script that outputs
true/false based on whether you're allowed to set affinity?
A: I don't feel comfortable providing this without the opportunity to test, but you can try something like this...
flags=$(cut -f 9 -d ' ' /proc/30084/stat)
echo $(($flags & 0x40000000))
I have a list of binary numbers in little-endian format in MATLAB workspace, and I want to convert them to int32. a is a double vector of zeroes and ones, like so:
a = [0 0 0 1 1 0 0 1 1 1 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1 1 1 1 1 0 0 0];
int32(a) gives me a row vector of the 32 binary values without converting it to 32 bit integer.
The solutions from this related question (which is specific to unsigned integers) can be modified to handle a signed integer. The easiest approach would be to convert the output from there to uint32, then convert to int32 using typecast. Building on this solution:
>> a = [0 0 0 1 1 0 0 1 1 1 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1 1 1 1 1 0 0 0];
>> b = typecast(uint32(sum(pow2(find(a)-1))), 'int32')
b =
int32
521688984
If you know the specific representation used for the bit pattern (my guess would be two's complement), you could avoid using typecast by accounting for the sign bit and complement directly in the calculations.
If a is an N-by-32 matrix, you can simply replace the sum(...) with the vectorized calculations from the linked solution:
b = typecast(uint32(a*(2.^(0:size(a, 2)-1)).'), 'int32');
I'm trying to initiate a matrix of 10x10 where the value of each cell is 0. But it looks like it's failing to create a second column when I initialize the matrix in this way.
$m = ,#(0) * 10
$m += ,#(0) * 10
$m | %{"$_"}
However, this method will set the rows correctly (but not initialize the cells at 0 like I want)
$m = ,#(1..10)
$m += ,#(1..10)
$m | %{"$_"}
expected output:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
And I would rather not iterate over the cells to reset them.
An explanation of why it only makes a single column when I do it the 1st way would also be helpful.
When you add somthing together, the left side of the expression will decide the result. Since the left side of the expression is an array, it will merge the array's content with the new value. 10 arrays + 10 arrays = your $m-array now contains 20 single-item arrays. It is a two-dimensional array/matrix, but it's 20x1 and not 2x10.
$m.Count
20
$m[0].Count
1
Be aware that arrays are reference type, so you are creating 10 copies of the reference (pointer) to the same single-item array, which means this will happen:
$m[0][0] = 1
$m | % {"$_"}
1
1
1
1
1
1
1
1
1
1
0
0
0
0
0
0
0
0
0
0
I would use foreach-loops to create ten arrays of ten items (using 3 in example to shorten demo).
$m = 1..3 | % { ,(1..3 | % { 0 }) }
$m.Count
3
$m[0].Count
3
$m[0][0] = 1
$m[2][1] = 1
$m | % {"$_"}
1 0 0
0 0 0
0 1 0
Frode F.'s helpful answer explains the problem with your solution attempt well.
However, you can efficiently create true, initialized-to-0 multi-dimensional arrays using New-Object:
$arr2d = New-Object 'int[,]' (10, 10) # Create a 10 x 10 [int] array, initialized to all 0s
Caveats:
Attempting to append to such an array with += quietly flattens it to a single-dimensional one, with the new element(s) appended - curiously, even type-constraining the variable ([int[,]] $arr2d = New-Object ...) cannot prevent that.
Range expressions such as 0..2 cannot be used in the indices of such an array.
Examine the resulting array:
Get the type:
> $arr2d.GetType().Name
Int32[,]
Get the rank (dimension count):
> $arr2d.Rank
2
Get the count of elements of each dimension:
> 0..($arr2d.Rank-1) | % { $arr2d.GetLength($_) }
10
10
Set some elements:
$arr2d[0,1] = 1; $arr2d[4,5] = 5; $arr2d[8,9] = 9
Enumerate the elements in matrix form:
> 0..$arr2d.GetUpperBound(0) |
% { $dim1=$_; (0..$arr2d.GetUpperBound(1) | % { $arr2d[$dim1, $_] }) -join ' ' }
0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 5 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 9
0 0 0 0 0 0 0 0 0 0
I am use the following command to list all users and there pending jobs in LSF:
busers -w all
Example output:
USER/GROUP JL/P MAX NJOBS PEND RUN SSUSP USUSP RSV MPEND
aaaaa - - 0 0 0 0 0 0 200
bbbbb - - 100 50 50 0 0 0 200
ccccc - - 1 0 1 0 0 0 200
I'm looking for a command that will only display the users whose NJOBS value is greater than 0, i.e. those who have actually submitted at least one job.
In the example, that would mean that only the lines for users 'bbbbb' and 'ccccc' should appear.
To check all users jobs the simpler method would be:
bjobs -u all
To check suspended ones just use:
bjobs -u all | grep SUSP