adding a new line to a series of data lines in Powershell

I have .cfg files each named the same, in many sub-folders, having the following structures :

--some text
light.0 = some text
light.1 = some text
light.2 = some text
...
light.n = some text
...some text

I want to add a (n+1)th data line following the last (n)th line of each .cfg file :

light.(n+1) = sonme text

Each .cfg may have varied last (n)th lines.

I tried the following code, yet it seems like it is of no avail so far :

# Get all the config files, and loop over them
Get-ChildItem "d:\test" -Recurse -Include *.cfg | ForEach-Object {

    # Output a progress message
    Write-Host "Processing file: $_"

    # Make a backup copy of the file, forcibly overwriting one if it's there
    Copy-Item -LiteralPath $_ -Destination "$_+.bak" -Force

    # Read the lines in the file
    $Content = Get-Content -LiteralPath $_ -Raw

    # A regex which matches the last "light..." line
    #  - line beginning with light.
    #  - with a number next (capture the number)
    #  - then equals, text up to the end of the line
    #  - newline characters
    #  - not followed by another line beginning with light
    $Regex = '^light.(?\d+) =.*?$(?![\r\n]+^light)'

    # A scriptblock to calculate the regex replacement
    # needs to output the line which was captured
    # and calculat the increased number
    # and output the new line as well
    $ReplacementCalculator = {

        param($RegexMatches)

        $LastLine = $RegexMatches[0].Value
        $Number = [int]$RegexMatches.groups['num'].value

        $NewNumber = $Number + 1

        "$LastLine`nlight.$NewNumber = some new text"

    }

    # Do the replacement and insert the new line
    $Content = [regex]::Replace($Content, $Regex, $ReplacementCalculator, 'Multiline')

    # Update the file with the new content
    $Content | Set-Content -Path $_

}

Do you have an actual file that we can look at? –Some text indicates these entries could be in the middle of an INI type file, so for parsing it’s better to show an example so that we’re not guessing how the rest of the file is formatted.

Short answer: YES. :wink:

A quick search in Google for ‘powershell add line to text file’ leads to something like this: Append text to a file using Add-Content.

one sample reflecting what each .cfg file look like :

[weight_and_balance]
max_gross_weight = 150000
empty_weight = 84170
reference_datum_position = 0, 0, 0
empty_weight_CG_position = -5, 0, 0
empty_weight_pitch_MOI = 3172439
empty_weight_roll_MOI = 2262183
empty_weight_yaw_MOI = 3337024
empty_weight_coupled_MOI = 0
max_number_of_stations = 9

[lights]

light.0 = 1, 1.100, 0.000, 9.567, fx_shockwave_beaconh
light.1 = 1, -5.242, 0.000, -4.216, fx_shockwave_beaconb
light.2 = 2, -72.316, 0.000, 7.650, fx_shockwave_strobe
light.3 = 3, -26.058, -55.418, 4.843, fx_shockwave_navwhi
light.4 = 3, -26.058, 55.418, 4.843, fx_shockwave_navwhi
light.5 = 3, -20.464, -55.628, 4.873, fx_shockwave_navred
light.6 = 2, -21.174, -56.167, 5.063, fx_shockwave_strobe
light.7 = 2, -21.174, 56.167, 5.063, fx_shockwave_strobe
light.8 = 3, -20.464, 55.628, 4.873, fx_shockwave_navgre
light.9 = 5, 6.800, -8.153, 0.060, fx_shockwave_landing_light_narrow
light.10 = 5, 6.800, 8.153, 0.060, fx_shockwave_landing_light_narrow
light.11 = 5, 6.632, -2.911, -3.742, fx_shockwave_landing_light_narrow
light.12 = 5, 6.632, 2.911, -3.742, fx_shockwave_landing_light_narrow
light.13 = 6, 48.247, 0.000, -5.958, fx_shockwave_landing_light_narrow
light.14 = 2, -5.800, -16.410, -2.880, fx_nicksmokeAI_737
light.15 = 2, -5.800, 16.410, -2.880, fx_nicksmokeAI_737

[GeneralEngineData]

//0=Piston, 1=Jet, 2=None, 3=Helo-Turbine, 4=Rocket, 5=Turboprop
engine_type = 1
fuel_flow_scalar = 1
min_throttle_limit = -0.25
max_contrail_temperature = -30
engine.0 = 4.75, -16.1, -4.5
engine.1 = 4.75,  16.1, -4.5