开发者

Add text to every line in text file using PowerShell

开发者 https://www.devze.com 2023-02-09 09:09 出处:网络
I\'d like to add characters to the end of every line of text in a .txt document. #Define Variables $a = c:\\foobar.txt

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 ap开发者_如何转开发pend-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.


No function necessary. This would do it:

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


Soemthing like this should work:

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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消