I got such stdout.
Queues
queue dur autoDel excl msg msgIn msgOut bytes bytesIn bytesOut cons bind
==============================================================================================================================
14531c8d-dd9b-4f41-9d92-c1344774d21c:0.0 Y Y 0 0 0 0 0 0 1 2
qmfagent-425fa29c-0892-4c08-a2d9-e7331a37dc13 Y Y 0 0 0 0 0 0 1 4
So I need to parse this output and get only something like this
14531c8d-dd9b-4f41-9d92-c1344774d21c:0.0
qmfagent-425fa29c-0892-4c08-a2d9-e7331a37dc13
Can anybody tell me how to do this ruby? Of course there could be more lines.
Split lines by newline (\n). Get last two lines.
output = <<EOD
Queues
queue dur autoDel excl msg msgIn msgOut bytes bytesIn bytesOut cons bind
==============================================================================================================================
14531c8d-dd9b-4f41-9d92-c1344774d21c:0.0 Y Y 0 0 0 0 0 0 1 2
qmfagent-425fa29c-0892-4c08-a2d9-e7331a37dc13 Y Y 0 0 0 0 0 0 1 4
EOD
lines = output.strip.split("\n") # Split lines by newline
last_two_lines = lines[-2..-1] # Get the last 2 lines.
p last_two_lines.map {|line| line.split[0]} # Get the first fields.
prints
["14531c8d-dd9b-4f41-9d92-c1344774d21c:0.0", "qmfagent-425fa29c-0892-4c08-a2d9-e7331a37dc13"]
queues = <<EOS
queue dur autoDel excl msg msgIn msgOut bytes bytesIn bytesOut cons bind
==============================================================================================================================
14531c8d-dd9b-4f41-9d92-c1344774d21c:0.0 Y Y 0 0 0 0 0 0 1 2
qmfagent-425fa29c-0892-4c08-a2d9-e7331a37dc13 Y Y 0 0 0 0 0 0 1 4
EOS
queues.lines.each {|line|
puts line.split.first if line =~ /[[\da-f]]{4}/i # detects 4 consecutive hexadecimals
}
Related
REFERENCE STRUCTURE = 00000 A,B,C = 120.000 120.000 42.560
ALPHA,BETA,GAMMA = 90.000 90.000 90.000 SPGR = P1
31984 1 new.pdb
x y z
1 C 8.17500 93.80900 21.90700 8 4 2 0 0 0 0 0 -0.036 1
2 C 9.34800 94.14800 22.73500 1 16 9 0 0 0 0 0 0.038 1
3 C 8.05800 95.47500 24.28800 6 9 15 0 0 0 0 0 0.038 1
4 C 6.95800 94.40500 22.32000 12 1 6 0 0 0 0 0 0.060 1
5 O 7.20600 96.40600 26.25200 15 0 0 0 0 0 0 0 -0.270 1
6 C 6.88800 95.13100 23.50200 4 10 3 0 0 0 0 0 -0.036 1
7 O 4.60000 94.52600 21.81800 1645872 0 0 0 0 0 0 0 -0.245 1
8 H 8.26600 93.17800 21.03500 1 0 0 0 0 0 0 0 0.063 1
9 C 9.25800 94.94800 23.85500 2 3 11 0 0 0 0 0 -0.037 1
10 H 5.98600 95.70100 23.66700 6 0 0 0 0 0 0 0 0.063 1
11 H 10.19600 95.24800 24.29800 9 0 0 0 0 0 0 0 0.063 1
12 C 5.70900 94.23600 21.42300 13454 7 4 0 0 0 0 0 0.337 1
13 O 5.87600 93.60100 20.21100 14 12 0 0 0 0 0 0 -0.477 1
14 H 5.04400 93.52600 19.73800 13 0 0 0 0 0 0 0 0.295 1
I have this file structure and I need to make all the columns after the x, y and z columns to be zero and the last column to be deleted. for example I need to have the following as output (sample).
1 C 8.17500 93.80900 21.90700 0 0 0 0 0 0 0 0 0
2 C 9.34800 94.14800 22.73500 0 0 0 0 0 0 0 0 0
If the pattern is predictable, a find/replace would work
%s/\v(\d+ \w+\s+([0-9\.]+\s+){3}).*/\10 0 0 0 0 0 0 0 0
Breakdown
%s/ - substitute every line
\v - very magic
(\d+ \w+\s+([0-9\.]+\s+){3}) - capture everything between ()
searches for '1 C 8.17500 93.80900 21.90700 '
.* - remaining character after our captured group
/\1 - replace, insert the captured group
0 0 0 0 0 0 0 0 0 - add required zero's
Regexbuddy comment
// (\d+ \w+\s+([0-9\.]+\s+){3}).*
//
// Options: Case insensitive; Exact spacing; Dot matches line breaks; ^$ match at line breaks; Default line breaks; Numbered capture; Allow duplicate names; Greedy quantifiers; Allow zero-length matches
//
// Match the regex below and capture its match into backreference number 1 «(\d+ \w+\s+([0-9\.]+\s+){3})»
// Match a single character that is a “digit” (ASCII 0–9 only) «\d+»
// Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Match the character “ ” literally « »
// Match a single character that is a “word character” (ASCII letter, digit, or underscore only) «\w+»
// Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Match a single character that is a “whitespace character” (ASCII space, tab, line feed, carriage return, vertical tab, form feed) «\s+»
// Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Match the regex below and capture its match into backreference number 2 «([0-9\.]+\s+){3}»
// Exactly 3 times «{3}»
// Match a single character present in the list below «[0-9\.]+»
// Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// A character in the range between “0” and “9” «0-9»
// The literal character “.” «\.»
// Match a single character that is a “whitespace character” (ASCII space, tab, line feed, carriage return, vertical tab, form feed) «\s+»
// Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Match any single character «.*»
// Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Let us consider the example:
The pbm file "imFile.pbm" contains the pixels as follows :
P1
# Comment
9 6
0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
How can I determine the width and Height of the image. I have used the following code but failed.
with open("imFile.pbm", 'rb') as f:
image = f.size
print image
f.close()
When I compiled it in my ubuntu14.04 os, it shows error. Any suggestion will be appreciated. Thank you in advance.
The width and height is right there in the file, in the first row after the first one, skipping comments. That's not what .size is for; you need to read and parse the file.
with open("imFile.pbm", 'r') as f:
lines = f.readlines()
lines.pop(0) # skip header
line = lines.pop(0) # get next line
while line.startswith("#"): # repeat till that line is not a comment
line = lines.pop(0)
width, height= line.split() # split the first non-comment lin
print("%s x %s" % (width, height)) # => 9 x 6
Let Y be a vector of length N, containing numbers from 1 to 10. As example code you can use:
Y = vec(1:10);
I am writing the code which must create an N x 10 matrix, each row consisting of all zeros except for a 1 only in the position which corresponds to the number in vector Y. Thus, 1 in Y becomes 10000000000, 3 becomes 0010000000, and so on.
This approach works:
cell2mat(arrayfun(#(x)eye(10)(x,:), Y, 'UniformOutput', false))
My next idea was to "optimize", so eye(10) is not generated N times, and I wrote this:
theEye = eye(10);
cell2mat(arrayfun(#(x)theEye(x,:), Y, 'UniformOutput', false))
However, now Octave is giving me error:
error: can't perform indexing operations for diagonal matrix type
error: evaluating argument list element number 1
Why do I get this error? What is wrong?
Bonus questions — do you see a better way to do what I am doing? Is my attempt to optimize making things easier for Octave?
I ran this code in Octave and eye creates a matrix of a class (or whatever this is) known as a Diagonal Matrix:
octave:3> theEye = eye(10);
octave:4> theEye
theEye =
Diagonal Matrix
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
In fact, the documentation for Octave says that if the matrix is diagonal, a special object is created to handle the diagonal matrices instead of a standard matrix: https://www.gnu.org/software/octave/doc/interpreter/Creating-Diagonal-Matrices.html
What's interesting is that we can slice into this matrix outside of the arrayfun call, regardless of it being in a separate class.
octave:1> theEye = eye(10);
octave:2> theEye(1,:)
ans =
Diagonal Matrix
1 0 0 0 0 0 0 0 0 0
However, as soon as we put this into an arrayfun call, it decides to crap out:
octave:5> arrayfun(#(x)theEye(x,:), 1:3, 'uni', 0)
error: can't perform indexing operations for diagonal matrix type
This to me doesn't make any sense, especially since we can slice into it outside of arrayfun. One may suspect that it has something to do with arrayfun and since you are specifying UniformOutput to be false, a cell array of elements is returned per element in Y and perhaps something is going wrong when storing these slices into each cell array element.
However, this doesn't seem to be the culprit either. I took the first three rows of theEye, placed them into a cell array and merged them together using cell2mat:
octave:6> cell2mat({theEye(1,:); theEye(2,:); theEye(3,:)})
ans =
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
As such, I suspect that it may be some sort of internal bug (if you could call it that...). Thanks to user carandraug (see comment above), this is indeed a bug and it has been reported: https://savannah.gnu.org/bugs/?47510. What may also provide insight is that this code runs as expected in MATLAB.
In any case, one thing you can take away from this is that I would seriously refrain from using cell2mat. Just use straight up indexing:
Y = vec(1:10);
theEye = eye(10);
out = theEye(Y,:);
This would index into theEye and extract out the relevant rows stored in Y and create a matrix where each row is zero except for the corresponding value seen in each element Y.
Also, have a look at this post for a similar example: Replace specific columns in a matrix with a constant column vector
However, it is defined over the columns instead of the rows, but it's very similar to what you want to achieve.
Another approach; We start with the data:
>> len = 10; % max number
>> vec = randi(len, [1 7]) % vector of numbers
vec =
1 10 9 5 7 3 6
Now we build the indicator matrix:
>> I = full(sparse(1:numel(vec), vec, 1, numel(vec), len))
I =
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
On Octave I'm trying to unpack a vector in the format:
y = [ 1
2
4
1
3 ]
I want to return a matrix of dimension ( rows(y) x max value(y) ), where for each row I have a 1 in the column of the original digits value, and a zero everywhere else, i.e. for the example above
y01 = [ 1 0 0 0
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0 ]
so far I have
y01 = zeros( m, num_labels );
for i = 1:m
for j = 1:num_labels
y01(i,j) = (y(i) == j);
end
end
which works, but is going get slow for bigger matrices, and seems inefficient because it is cycling through every single value even though the majority aren't changing.
I found this for R on another thread:
f3 <- function(vec) {
U <- sort(unique(vec))
M <- matrix(0, nrow = length(vec),
ncol = length(U),
dimnames = list(NULL, U))
M[cbind(seq_len(length(vec)), match(vec, U))] <- 1L
M
}
but I don't know R and I'm not sure if/how the solution ports to octave.
Thanks for any suggestions!
Use a sparse matrix (which also saves a lot of memory) which can be used in further calculations as usual:
y = [1; 2; 4; 1; 3]
y01 = sparse (1:rows (y), y, 1)
if you really want a full matrix then use "full":
full (y01)
ans =
1 0 0 0
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0
Sparse is a more efficient way to do this when the matrix is big.
If your dimension of the result is not very high, you can try this:
y = [1; 2; 4; 1; 3]
I = eye(max(y));
y01 = I(y,:)
The result is same as full(sparse(...)).
y01 =
1 0 0 0
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0
% Vector y to Matrix Y
Y = zeros(m, num_labels);
% Loop through each row
for i = 1:m
% Use the value of y as an index; set the value matching index to 1
Y(i,y(i)) = 1;
end
Another possibility is:
y = [1; 2; 4; 1; 3]
classes = unique(y)(:)
num_labels = length(classes)
y01=[1:num_labels] == y
With the following detailed printout:
y =
1
2
4
1
3
classes =
1
2
3
4
num_labels = 4
y01 =
1 0 0 0
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0
I am having data but it is not properly formatted. How can I properly arrange it into proper columns using Jython?
Ex:
`Active Threads/Pool Size/Max Pool WEB: ORB: Activetran CPU
ABC 1:20:100 0:1:50 0 1
EFG 1:19:100 0:2:50 0 1
MSG 0:0:50 0:0:50 0 0
ABAAASAA 1:2:50 0:10:50 0 0
BBASADDADADDAB 0:0:50 0:0:50 0 0
BVA 1:20:100 0:1:50 0 1`
n1 = 1
n2 = 2
s = 'a'
print '%4d : %4d : %4s' % (n1, n2, s)
For more details see: http://docs.python.org/2/library/stdtypes.html#string-formatting