简体   繁体   English

如何使用 Microsoft PowerShell 修改创建的 output?

[英]How to modify created output using Microsoft PowerShell?

I've created a code in PowerShell to search through files to find lines containing null values '000' and create a new file that replaces these values with a user input.我在 PowerShell 中创建了一个代码来搜索文件以查找包含 null 值“000”的行,并创建一个新文件,用用户输入替换这些值。 I've included the code below and it does function as intended.我已经包含了下面的代码,它按预期执行了 function。

The problem I'm having is that it also prints 'line' and the command is also repeated before and after the modified data (This time using an input of '5u7').我遇到的问题是它还打印“行”,并且该命令也在修改数据之前和之后重复(这次使用输入“5u7”)。 This is demonstrated in the output I've pasted below the code.这在我粘贴在代码下方的 output 中得到了证明。

I would like to remove these extra lines and also add my own string above the data to show the data has been modified.我想删除这些额外的行,并在数据上方添加我自己的字符串以显示数据已被修改。 Hopefully to output something like this:希望 output 是这样的:

THIS DATA HAS BEEN MODIFIED此数据已修改

--- aaa --- 5u7 --- ccc --- ddd --- aaa --- 5u7 --- ccc --- ddd
--- aaa --- 5u7 --- ccc --- ppp --- aaa --- 5u7 --- ccc --- ppp
--- aaa --- 5u7 --- ccc --- ddd --- aaa --- 5u7 --- ccc --- ddd
--- aaa --- 5u7 --- ccc --- zzz --- aaa --- 5u7 --- ccc --- zzz

Set-ExecutionPolicy AllSigned
#Set Execution Policy

ls -r -Path C:\Users\Desktop\Data\Raw_data | sls '000' | select Line | 
Out-File C:\Users\Desktop\Data\Raw_data\Processed_data.txt

#Extract lines with null values and create a new file for this data

$file = @(get-item -Path 
"C:\Users\Desktop\Data\Raw_data\Processed_data.txt")

#Defines new file for for loop

$replacementStr = Read-Host -Prompt 'Input data in xxx format'

#Requests input from user to replace null values

$confirmation = Read-Host "Is this correct? (y/n):"

#Confirmation that format is correct

if ($confirmation -eq'n'){

    Remove-Item C:\Users\Desktop\Data\Raw_data\Processed_data.txt
    # exit 
}

#If format is incorrect delete created file and end the script

elseif ($confirmation -eq'y') {
    for ($file) 
        { 
            (Get-Content $file) | 
                    Foreach-object { $_ -replace '000' , $replacementStr}| 
                Set-Content $file 
                Write-Host Processed $file
                break
        }
}

#Search through the file for the 000 values and replace with the new 
user input string
Line                                                                                                                                          
----                                                                                                                                          
ls -r -Path C:\Users\Desktop\Data\Raw_data | sls '5u7' | select Line | Out-File C:\Users\Desktop\Data\Raw_data\Processed_data.txt
               foreach-object { $_ -replace '5u7' , $replacementStr   } |                                                                    
--- aaa --- 5u7 --- ccc --- ddd                                                                                                               
--- aaa --- 5u7 --- ccc --- ppp                                                                                                               
--- aaa --- 5u7 --- ccc --- ddd                                                                                                               
--- aaa --- 5u7 --- ccc --- zzz                                                                                                               
ls -r -Path C:\Users\Desktop\Data\Raw_data | sls '5u7' | select Line | Out-File C:\Users\Desktop\Data\Raw_data\Processed_data.txt   

the command命令

ls -r -Path C:\Users\Desktop\Data\Raw_data | sls '000' | select Line

does select the column/property "Line" and also will return an object with that very property. select 列/属性“行”是否会返回具有该属性的 object。 That's why it's printed out.这就是它被打印出来的原因。 To avoid this behaviour you can use the parameter -ExpandProperty like so to return only the contents of the selection:为了避免这种行为,您可以像这样使用参数-ExpandProperty来仅返回选择的内容:

ls -r -Path C:\Users\Desktop\Data\Raw_data | sls '000' | select -ExpandProperty Line

To add a custom property name you can set a new property and add the contents of the property "Line" to it.要添加自定义属性名称,您可以设置一个新属性并将属性“Line”的内容添加到它。

ls -r -Path C:\Users\Desktop\Data\Raw_data | sls '000' | select -Property @{Name = 'THIS DATA HAS BEEN MODIFIED'; Expression = {$_.Line}}

Please note that it is not best practice to use aliases (like "ls" - use "Get-ChildItem" instead as well as "sls" - "Select-String") and also avoid shortening parameters ("-r" should be "-Recurse").请注意,使用别名(如“ls” - 使用“Get-ChildItem”以及“sls” - “Select-String”)并不是最佳实践,并且还要避免缩短参数(“-r”应该是“ -递归”)。 This makes it more robust and better readable.这使它更健壮和更好的可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM