My requirement is to find the location of a particular string in a line from notepad file but while reading it in powershell additional space get added that's why not able to find the location of a particular string. How I can find the location is this case??
I am using this code for achieving this
$ParamsPathForData = ($dir + "\TimeStats\TimeStats_1slot\29_12_2015_07TimeStats1.txt")
$data = Get-Content $ParamsPathForData
write-host $data.count total lines read from file
foreach ($line in $data)
{
$l =$line.IndexOf("12/29/2015")
write-host $l
}
I am reading this line from notepad ->
TimeStats 29 12/29/2015 7:13:42 AM +00:00 Debug PREPROCESS: SlotNo:
325-00313, Ip Address: 10.2.200.15, Duplicate Message: False,
Player-Card-No: , MessageId: 883250003130047966, MessageName:
GameIdInfo, Thread Init Delay: 14, Time To Parse: 155, Time To Exec
Main Workflow: 424, Time To Construct & send Response: 22, Total
Response Time: 615
But while exceuting it in powershell i am getting this with additinal spaces ->
T i m e S t a t s 2 9 1 2 / 2 9 / 2 0 1 5 7 : 1 3 : 4 2 A M
+ 0 0 : 0 0 D e b u g P R E P R O C E S S : S l o t N o : 3 2 5 - 0 0 3 1 3 , I p A d d r e s s : 1 0 . 2 . 2 0 0 . 1 5 , D
u p l i c a t e M e s s a g e : F a l s e , P l a y e r
- C a r d - N o : , M e s s a g e I d : 8 8 3 2 5 0 0 0 3 1 3 0 0 4 7 9 6 6 , M e s s a g e N a m e : G a m e I d I n f o , T h
r e a d I n i t D e l a y : 1 4 , T i m e T o P a r s e :
1 5 5 , T i m e T o E x e c M a i n W o r k f l o w : 4 2
4 , T i m e T o C o n s t r u c t & s e n d R e s p o n s
e : 2 2 , T o t a l R e s p o n s e T i m e : 6 1 5
Anybody please help me???
Change the the encoding to Unicode...
$data = Get-Content $ParamsPathForData -Encoding Unicode
Related
I'm using nssm.exe in my scripts to manage the windows services. But in PowerShell, the command output is coming with spaces after every alphabet.
PS> $nssm = (Get-Command D:\nssm.exe)
PS> & $nssm
nssm.exe : N S S M : T h e n o n - s u c k i n g s e r v i c e m a n a g e r
At line:1 char:1
+ & $nssm
+ ~~~~~~~
+ CategoryInfo : NotSpecified: (N S S M : T h... m a n a g e r :String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
V e r s i o n 2 . 2 4 3 2 - b i t , 2 0 1 4 - 0 8 - 3 1
U s a g e : n s s m < o p t i o n > [ < a r g s > . . . ]
T o s h o w s e r v i c e i n s t a l l a t i o n G U I :
n s s m i n s t a l l [ < s e r v i c e n a m e > ]
T o i n s t a l l a s e r v i c e w i t h o u t c o n f i r m a t i o n :
n s s m i n s t a l l < s e r v i c e n a m e > < a p p > [ < a r g s > . . . ]
T o s h o w s e r v i c e e d i t i n g G U I :
n s s m e d i t < s e r v i c e n a m e >
How to get the output without such wide-format spaces among alphabets?
Set up
I am using the google OR tools as a constraint programming solver:
from ortools.sat.python import cp_model
I have defined the following BoolVars
model = cp_model.CpModel()
a = model.NewBoolVar("a")
b = model.NewBoolVar("b")
c = model.NewBoolVar("c")
d = model.NewBoolVar("d")
e = model.NewBoolVar("e")
f = model.NewBoolVar("f")
g = model.NewBoolVar("g")
Question
I need to add a complex boolean constraint to the model. Something like
(a || b) && (d || e) == g
How can I add a complex boolean constraint like this to the model?
PS: I couldn't find this information immediately online, but found a solution based on an answer I got on a related problem here and another related problem of another person here. I summarize my findings here in Q&A style in the hope that they will be useful for someone.
This kind of constraint cannot be added at once to the model. The constraint needs to be split up in its components (and & or gates). Each basic component needs to be set equal to a new BoolVar. These are then combined to add the final constraint.
Breakdown to basic components
We'll do this as follows:
(a || b) == c
(d || e) == f
(c && f) == g
This last equation is equivalent to the original equation:
(a || b) && (d || e) == g
Storing basic constraints in new BoolVars
An "OR" type of equation can be evaluated with the following function:
def evaluate_or(a, b, c):
# Either a or b or both must be 1 if c is 1.
model.AddBoolOr([a, b]).OnlyEnforceIf(c)
# The above line is not sufficient, as no constraints are defined if c==0 (see reference documentation of
# "OnlyEnforceIf". We therefore add another line to cover the case when c==0:
# a and b must both be 0 if c is 0
model.AddBoolAnd([a.Not(), b.Not()]).OnlyEnforceIf([c.Not()])
An "AND" type of equation can similarily be evaluated with the following function:
def evaluate_and(a, b, c):
# Both a and b must be 1 if c is 1
model.AddBoolAnd([a, b]).OnlyEnforceIf(c)
#What happens if c is 0? This is still undefined thus let us add another line:
# Either a or b or both must be 0 if c is 0.
model.AddBoolOr([a.Not(), b.Not()]).OnlyEnforceIf(c.Not())
In this specific case the basic constraints are twice an OR gate. We store them in c and f:
evaluate_or(a, b, c)
evaluate_or(d, e, f)
Adding the complex constraint
This is now really simple and can be done using the BoolVars from the previous step and an AND gate:
evaluate_and(c, f, g)
Discussion
Any complex constraint can be added using intermediary BoolVars and the defined OR and AND gate functions above. The constraint just needs to be broken down into its basic components.
Full program
This is the full program:
from ortools.sat.python import cp_model
model = cp_model.CpModel()
a = model.NewBoolVar("a")
b = model.NewBoolVar("b")
c = model.NewBoolVar("c")
d = model.NewBoolVar("d")
e = model.NewBoolVar("e")
f = model.NewBoolVar("f")
g = model.NewBoolVar("g")
def evaluate_or(a, b, c):
# Either a or b or both must be 1 if c is 1.
model.AddBoolOr([a, b]).OnlyEnforceIf(c)
# The above line is not sufficient, as no constraints are defined if c==0 (see reference documentation of
# "OnlyEnforceIf". We therefore add another line to cover the case when c==0:
# a and b must both be 0 if c is 0
model.AddBoolAnd([a.Not(), b.Not()]).OnlyEnforceIf([c.Not()])
def evaluate_and(a, b, c):
# Both a and b must be 1 if c is 1
model.AddBoolAnd([a, b]).OnlyEnforceIf(c)
#What happens if c is 0? This is still undefined thus let us add another line:
# Either a or b or both must be 0 if c is 0.
model.AddBoolOr([a.Not(), b.Not()]).OnlyEnforceIf(c.Not())
#Add the constraints
evaluate_or(a, b, c)
evaluate_or(d, e, f)
evaluate_and(c, f, g)
# Solve the model.
solver = cp_model.CpSolver()
solver.parameters.enumerate_all_solutions = True
status = solver.Solve(model, cp_model.VarArraySolutionPrinter([a, b, c, d, e, f, g]))
Output
The following output is obtained:
Solution 0, time = 0.00 s
a = 0 b = 0 c = 0 d = 1 e = 0 f = 1 g = 0
Solution 1, time = 0.00 s
a = 0 b = 0 c = 0 d = 1 e = 1 f = 1 g = 0
Solution 2, time = 0.00 s
a = 0 b = 0 c = 0 d = 0 e = 1 f = 1 g = 0
Solution 3, time = 0.00 s
a = 0 b = 0 c = 0 d = 0 e = 0 f = 0 g = 0
Solution 4, time = 0.00 s
a = 0 b = 1 c = 1 d = 0 e = 0 f = 0 g = 0
Solution 5, time = 0.00 s
a = 0 b = 1 c = 1 d = 1 e = 0 f = 1 g = 1
Solution 6, time = 0.00 s
a = 0 b = 1 c = 1 d = 1 e = 1 f = 1 g = 1
Solution 7, time = 0.00 s
a = 0 b = 1 c = 1 d = 0 e = 1 f = 1 g = 1
Solution 8, time = 0.00 s
a = 1 b = 1 c = 1 d = 0 e = 1 f = 1 g = 1
Solution 9, time = 0.00 s
a = 1 b = 1 c = 1 d = 1 e = 1 f = 1 g = 1
Solution 10, time = 0.00 s
a = 1 b = 1 c = 1 d = 1 e = 0 f = 1 g = 1
Solution 11, time = 0.00 s
a = 1 b = 1 c = 1 d = 0 e = 0 f = 0 g = 0
Solution 12, time = 0.00 s
a = 1 b = 0 c = 1 d = 0 e = 0 f = 0 g = 0
Solution 13, time = 0.00 s
a = 1 b = 0 c = 1 d = 0 e = 1 f = 1 g = 1
Solution 14, time = 0.00 s
a = 1 b = 0 c = 1 d = 1 e = 1 f = 1 g = 1
Solution 15, time = 0.00 s
a = 1 b = 0 c = 1 d = 1 e = 0 f = 1 g = 1
I simplified the original problem up to this point
((P∧¬R)∨(¬Q∨R))∧((Q∧¬R)∨(¬P∨R))
, and I got stuck here. What would be the next step? Thanks for the help!!
I am solving it with you.
Hint-1: ((P∧Q)∨R) = (PVR) ∧ (QVR)
Hint-2: P ∧ True = P
Hint-3: P V True = True
Answer
It would be true in the end. Check it once.
Next step would be
= [(P V (~Q V R)) ^ ( ~R V (~Q V R))]
^[(Q V (~P V R)) ^ ( ~R V (~P V R))]
= (P V ~Q V R) ^ ( ~p V Q V R)
= R V ( (P V ~Q) ^ ( Q V ~P))
= R v (( Q -> P ) ^ ( P -> Q))
= R V (P <-> Q)
whenever R is True it is True.
Else
P Q P<->Q
------------------
F F T
F T F
T F F
T T T
So it conforms to the truth table. Shown above by trincot.
The following expressions are equivalent:
(p ⇒ r) ⇔ (q ⇒ r)
(¬p v r) ⇔ (¬q v r)
(¬p ⇔ ¬q) v r
(p ⇔ q) v r
(p ∧ q) v (¬p ∧ ¬q) v r
Truth table:
p q r result
---------------
0 0 0 1
0 0 1 1
0 1 0 0
0 1 1 1
1 0 0 0
1 0 1 1
1 1 0 1
1 1 1 1
I have searched and attempted to solve this puzzle myself (I've gotten close, but I've had no luck). I have a large table of values (composed of Sets of Values) that can have multiple combinations, but those combinations must be returned in the ID order.
I have not been able to get this to work in SQL.
Example Set:
(Sorry I am not able to post an image which would explain it better so Ill keep it simple.)
Table[(ID, Value) {(1,A),(1,B),(1,C),(2,D),(3,F),(3,G), (4,J), (5,S),(5,T),(5,U))}
RESULTS
ID VALUE
1 A
2 F
3 G
4 J
5 S
1 A
2 F
3 G
4 J
5 T
1 A
2 F
3 G
4 J
5 U
1 A
2 F
3 H
4 J
5 S
1 A
2 F
3 H
4 J
5 T
1 A
2 F
3 H
4 J
5 U
1 B
2 F
3 G
4 J
5 S
1 B
2 F
3 G
4 J
5 T
1 B
2 F
3 G
4 J
5 U
1 B
2 F
3 H
4 J
5 S
1 B
2 F
3 H
4 J
5 T
1 B
2 F
3 H
4 J
5 U
1 C
2 F
3 G
4 J
5 S
1 C
2 F
3 G
4 J
5 T
1 C
2 F
3 G
4 J
5 U
1 C
2 F
3 H
4 J
5 S
1 C
2 F
3 H
4 J
5 T
1 C
2 F
3 H
4 J
5 U
Here's the problem in dynamic SQL without any cursors or loops.
IF OBJECT_ID('yourTable') IS NOT NULL
DROP TABLE yourTable;
CREATE TABLE yourTable (ID INT, Value CHAR(1));
INSERT INTO yourTable
VALUES (1,'A'),(1,'B'),(1,'C'),
(2,'D'),
(3,'F'),(3,'G'),
(4,'J'),
(5,'S'),(5,'T'),(5,'U');
DECLARE #row_number_cols VARCHAR(MAX),
#Aliased_Cols VARCHAR(MAX),
#Cross_Joins VARCHAR(MAX),
#Unpivot VARCHAR(MAX);
SELECT #row_number_cols = COALESCE(#row_number_cols + ',','') + col,
#Aliased_Cols = COALESCE(#Aliased_Cols + ',','') + CONCAT(col,' AS col',ID),
#Cross_Joins = COALESCE(#Cross_Joins,'') + CASE
WHEN ID = 1 THEN CONCAT(' FROM (SELECT * FROM yourTable WHERE ID = 1) AS ID',ID)
ELSE CONCAT(' CROSS JOIN (SELECT * FROM yourTable WHERE ID = ',ID,') AS ID',ID)
END,
#Unpivot = COALESCE(#Unpivot + ',','') + CONCAT('col',ID)
FROM yourTable A
CROSS APPLY (SELECT CONCAT('ID',ID,'.Value')) CA(col) --Just so I can reuse "col" in my code
GROUP BY A.ID,CA.col
SELECT #row_number_cols,#Aliased_Cols,#Cross_Joins,#Unpivot
SELECT
'WITH CTE_crossJoins
AS
(
SELECT ROW_NUMBER() OVER (ORDER BY ' + #row_number_cols + ') group_num,' + #Aliased_Cols +
#Cross_Joins + '
)
SELECT group_num,
val
FROM CTE_crossJoins
UNPIVOT
(
val for col IN (' + #Unpivot + ')
) unpvt
ORDER BY 1,2'
Results:
group_num val
-------------------- ----
1 A
1 D
1 F
1 J
1 S
2 A
2 D
2 G
2 J
2 S
3 A
3 D
3 G
3 J
3 T
4 A
4 D
4 F
4 J
4 T
5 A
5 D
5 F
5 J
5 U
6 A
6 D
6 G
6 J
6 U
7 B
7 D
7 G
7 J
7 S
8 B
8 D
8 F
8 J
8 S
9 B
9 D
9 F
9 J
9 T
10 B
10 D
10 G
10 J
10 T
11 B
11 D
11 G
11 J
11 U
12 B
12 D
12 F
12 J
12 U
13 C
13 D
13 F
13 J
13 S
14 C
14 D
14 G
14 J
14 S
15 C
15 D
15 G
15 J
15 T
16 C
16 D
16 F
16 J
16 T
17 C
17 D
17 F
17 J
17 U
18 C
18 D
18 G
18 J
18 U
I think this has been answered before here:
How to generate all possible data combinations in SQL?
difference being that they essentially dropped the ID column, should be easy to pull it through though.
You can employ the SQL windows function to achieve this.
;WITH CTE AS
(
SELECT Id,
Value,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) RN
FROM Tbl
)
SELECT * FROM CTE ORDER BY RN, ID, VALUE
Fiddle
I have two diagonal matrices. I am trying to build a larger block diagonal matrix from them. For example if I had this:
D = diag(zeros(3,1)+1)
D =
1 0 0
0 1 0
0 0 1
and...
E = diag(zeros(2,1)+2, -1) + diag(zeros(2,1)+2, +1) + diag(zeros(3,1)+4)
E =
4 2 0
2 4 2
0 2 4
I have an equation that says A*U = X
Where A is
[E D 0
D E D
0 D E]
This is for 3x3. 5x5 would look like this:
A =
[E D 0 0 0
D E D 0 0
0 D E D 0
0 0 D E D
0 0 0 D E]
A would be another diagonal matrix consisting of these matrices. I need to produce a 40x40 and it would take a VERY LONG TIME to do manually, of course.
How can I define that? I haven't figured out how to use blkdiag to construct.
I solved this on my own manually because I could never find a Matlab function to help me.
for n = 1:Distance_Resolution
A(((n-1)*Distance_Resolution +1):n*Distance_Resolution, ((n-1)*Distance_Resolution +1):n*Distance_Resolution) = A1;
if n == Distance_Resolution
else
A((n*Distance_Resolution+1):(n+1)*(Distance_Resolution), ((n-1)*Distance_Resolution+1:n*Distance_Resolution)) = A2;
A((n-1)*Distance_Resolution+1:n*Distance_Resolution, (n*Distance_Resolution+1):(n+1)*(Distance_Resolution)) = A2;
end
end
This will produce a block matrix that has the above specified demands and is of length Distance_Resolution x Distance_Resolution x Distance_Resolution. I defined A1 and A2 through help from above poster (Fo is just a constant here):
vector = zeros(Distance_Resolution,1) - Fo;
A2 = diag(vector);
A1 = toeplitz([1+4*Fo, -Fo, zeros(1,Distance_Resolution-2)]);
This is a workable code snippet, but I am still looking for a smarter way to code it.