Powershell,从txt文件中读取并格式化数据(删除行,删除它们之间的空格)
问题描述:
我对 powershell 真的很陌生.我想用powershell读取一个txt文件,改成另一种格式.
I am really very new to powershell. I want to use powershell to read a txt file and change it to another format.
- 从 txt 文件中读取.
- 格式化数据(删除行,删除中间的空格)
- 记录数(T 000000002"9 个字符)
然后将输出写入新文件.
and then write the output to a new file.
两天前我刚刚启动了 powershell,所以我还不知道如何做到这一点.
I just started powershell two days ago so I don't know how to do this yet.
答
function Count-Object() {
begin {
$count = 0
}
process {
$count += 1
}
end {
$count
}
}
$a= get-content .\members.txt |
Foreach-Object { ($_ -replace '\s','') } |
Foreach-Object { ($_ -replace '-','') } |
Foreach-Object { ($_ -replace 'OP_ID','') } |
Foreach-Object { ($_ -replace 'EFF_DT','') } |
Where-Object { $_ -ne '' }|
set-content .\newmembers.txt
$b = Get-Content .\newmembers.txt |
Count-Object $b
"T {0:D9}" -f $b | add-content .\newmembers.txt