小编use*_*978的帖子

如何使用 Powershell 从文本文件中删除回车符?

我使用以下命令将目录的内容输出到 txt 文件:

$SearchPath="c:\searchpath"
$Outpath="c:\outpath"

Get-ChildItem "$SearchPath" -Recurse | where {!$_.psiscontainer} | Format-Wide -Column 1'
| Out-File "$OutPath\Contents.txt" -Encoding ASCII -Width 200
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我最终得到的是一个包含我需要的信息的 txt 文件,但它添加了许多我不需要的回车符,使输出更难以阅读。

它看起来是这样的:

    c:\searchpath\directory

name of file.txt

name of another file.txt


    c:\searchpath\another directory

name of some file.txt
Run Code Online (Sandbox Code Playgroud)

这使得一个 txt 文件需要大量滚动,但实际信息并不多,通常不到一百行。

我希望它看起来像:

  c:\searchpath\directory
nameoffile.txt
  c:\searchpath\another directory
another file.txt
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止所尝试过的,但不起作用

$configFiles=get-childitem "c:\outpath\*.txt" -rec
foreach ($file in $configFiles)
{
(Get-Content $file.PSPath) | 
Foreach-Object {$_ -replace "'n", ""} | 
Set-Content $file.PSPath
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过 'r 但两个选项都会使文件保持不变。

另一种尝试:

Select-String -Pattern "\w" -Path 'c:\outpath\contents.txt' …
Run Code Online (Sandbox Code Playgroud)

powershell scripting replace find

4
推荐指数
1
解决办法
2万
查看次数

使用PowerShell基于另一列中的信息添加(数学)CSV列

我很难描述我在标题中需要什么,所以如果这完全没有意义,我提前道歉.

如果我有一个包含2个列的CSV,一个带有人名,另一个带有数字值,我需要在名称列中找到重复项,然后将该人的数值加在一起以获得新的总数CSV.

这是真实CSV的非常简化版本

Name,Number
Dog,1
Cat,2
Fish,1
Dog,3
Dog,2
Cat,2
Fish,1
Run Code Online (Sandbox Code Playgroud)

鉴于上述信息,我希望能够产生的是:

Name,Number
Dog,6
Cat,4
Fish,2
Run Code Online (Sandbox Code Playgroud)

我真的不知道如何到达那里,或者是否有可能使用PowerShell.我只能使用group-object按名称进行分组,但我不知道如何在此之后添加列.

我在研究这个问题时遇到的最大问题是,谷歌搜索时获得的大部分结果都涉及到向csv添加新列而不执行数学计算.

csv math powershell addition

4
推荐指数
1
解决办法
3580
查看次数

标签 统计

powershell ×2

addition ×1

csv ×1

find ×1

math ×1

replace ×1

scripting ×1