linux - PowerShell 脚本查找文件中的特定单词并在行开头添加 “4”

标签 linux powershell unix awk sed

I am the best.  Page
I am good.       Page
I am funny.       Page

Output:

4 I am the best.  Page
4 I am good.       Page
4 I am funny.       Page

PowerShell script needs to look for “page” and add “4” at the beginning of the line. I have created this script:

powershell -Command “sed ‘s/^Page/4 &/‘c:\users*.txt >test.txt”

但它在 PowerShell 中不起作用。

最佳答案

这应该可以做到:

$content = Get-Content "C:\path\to\my\file.txt" 
$newcontent = $null

Foreach($line in $content)
{
    if($line -ne "")
    {
        $line = "4 "+"$line`r`n"
        $newcontent += $line
    }
    else
    {
        $newcontent += "`r`n"
    }
} 

Set-Content -Path "C:\path\to\my\file.txt" -Value $newcontent

关于linux - PowerShell 脚本查找文件中的特定单词并在行开头添加 “4”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47800405/

相关文章:

python - 从 Powershell 启动 Python

shell - 按行上的字数对海量文件的行进行排序(最好是并行的)

linux - Grep 带空格的单词

c++ - 将编程鼠标事件发送到 X

python - 使用未安装的模块通过 ssh 运行远程 python 代码

linux - wkhtmltoimage 将 html 转换为图像 Perl 和 Centos

PHP 多进程消失了?

sql-server - 使用 “.ctor”参数调用 “2”的异常: “SetParent failed for Login ' XXX'。”

Python 无法使用 Powershell 找到路径

unix - 计数没有。 Unix中文件中一行的分隔符