Add text to every line in text file using PowerShell

耗尽温柔 提交于 2019-12-04 02:50:34

问题


I'd like to add characters to the end of every line of text in a .txt document.

#Define Variables
$a = c:\foobar.txt
$b = get-content $a

#Define Functions
function append-text  
    {  
    foreach-Object  
        {  
        add "*"  
        }  
    }  

#Process Code
$b | append-text

Something like that. Essentially, load a given text file, add a "*" the the end of every single line of text in that text file, save and close.


回答1:


Soemthing like this should work:

function append-text { 
  process{
   foreach-object {$_ + "*"}
    } 
  }



回答2:


No function necessary. This would do it:

$b|foreach {$_ +  "*"}



回答3:


PS> (gc c:\foobar.txt) -replace '\S+$','$&*'



回答4:


Simply took about 2 hours to work it out, had never used Powershell before, but here you go:

cls
#Define Functions
(gc g:\foobar.txt) -replace '\S+$','$& 1GB RAM 1x 1 GB Stick' | out-file "g:\ram 6400s.txt"

Change the file location. First file is the file you want to edit. The secound one is the output file.



来源:https://stackoverflow.com/questions/4952535/add-text-to-every-line-in-text-file-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!