【问题标题】:How do i use .txt input and output for math in powershell?如何在 powershell 中使用 .txt 输入和输出进行数学运算?
【发布时间】:2018-09-12 17:01:05
【问题描述】:

我需要使用 .txt 作为 powershell 命令的输入,并且需要将其输出到新行中的同一文件。
我应该使用什么样的命令?
我只尝试过基本的东西,例如

C:\"bunch O'shit"\input.txt *2   

"C:\"bunch O'shit"\input.txt" *2  

这两种方法都没有奏效。
麻烦各位。

【问题讨论】:

标签: powershell


【解决方案1】:

您需要先阅读文件的内容,然后才能开始处理它。要使用 PowerShell 读取文件,您可以使用 Get-Contentcmdlet。

获得文件内容后,您需要将其转换为可用于计算的类型,即您需要进行类型转换。

假设变量$num 是一个字符串,你可以这样做:

[int]$num

这将从字符串中创建一个整数。

您可以使用Get-Membercmdlet 检查变量的类型。

将所有这些放在一起:

# For the sake of it, let's create a file containing a single number
"2"  | Out-File -FilePath .\number.txt

# Let's read the file we just created and save the result in a variable
$num = Get-Content .\number.txt

# Do the type cast and multiply the result with another number
[int]$num * 2

您可以随时将结果通过管道传输到Get-Member 并检查它是什么类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多