问题
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