Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Math from a file.txt

    $space =("`r`n")
    $data = @(Get-Content C:\Users\user1\Desktop\ma.txt)
    $Array = ($Data.Split($space)).Split($space)
    $pos1 = $Array[0][0]+$Array[0][1]
    $pos2 = $Array[1][0]+$Array[1][1]
    $pos3 = $Array[2][0]+$Array[2][1]
    #$pos1
    #$pos2 
    #$pos3
    $zahl1 = $Array[0][5]+$Array[0][7]+$Array[0][9]
    $zahl1

PowerShell 7.2

txt1.txt has the text:

x1 = 2 + 3

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

x2 = 8 / 4

x3 = 1 – 4

i want the results (from x1,x2,x3) to be saved at txt2.txt with a command in Terminal.
I tried whith Arrays,
but i only get :2+3 instead of 5

Any thoughts?

>Solution :

You could use Invoke-Expression for this, but read the warning first

Get-Content -Path text1.txt | Where-Object {$_ -match '\S'} | ForEach-Object {
    $var,$calculation = ($_ -split '=').Trim()
    '{0} --> {1}' -f $var, (Invoke-Expression -Command $calculation)
} | Set-Content -Path text2.txt
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading