insert line in the text [SOLVED]

by lopyeg at 2013-01-22 08:39:17

Hello guys,
I am sure that the question is easy for you, but i just started study loops in PoSh. So
I have a TXT file with conext like
########
#London#
########
street1
street2
street3
########
#Glasgo#
########
street1
street2
street3

I need to insert new street in London section
the line ######## is not uniqe

i tried in this way
$Patern = “#London#”
$FileOriginal = “somefile.txt”
# create empty Array and use it as a modified file… #
[String[]] $FileModified = @()

Foreach ($Line in $FileOriginal)
{
$FileModified += $Line
if ($Line -match $patern)
{
#Add Lines after the selected pattern
$FileModified += $Line
$FileModified +=“new street1”
$FileModified +=“new street2”
}
}
Set-Content $fileName $FileModified



but nothing works

thank you for suggestions
by DonJ at 2013-01-22 08:44:32
The logic on this is going to be pretty ugly because of the nature of the file. You have the right approach, but don’t use -Match. Use -Eq instead. I also don’t see where you’re loading $FileOriginal. Normally, you’d use Get-Content to load the file. Right now, $FileOriginal only contains a filename. That’s why your script isn’t doing anything.
by lopyeg at 2013-01-23 00:13:01
or do i need to count all lines
get the number of patern line
and add the text into the line with nubmer of (paternLine.Nubmer+2) ?
by lopyeg at 2013-01-23 03:00:51
loops are understandable for me ((
i’ve done like this

$PatternNumber =(Select-String -Path $filename -Pattern “London”).linenumber

$FileOriginal[($PatternNumber+1)] += “rn new street rn new street2”