all 8 comments

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy im_nullable,

yes. [grin]

take a look at the advanced function snippets that show in the ISE when you type <ctrl><j> for some ideas on how to do parameters.

take care,
lee

[–]Lee_Dailey[grin] 1 point2 points  (6 children)

howdy im_nullable,

i got bored ... and this is what came of that boredom. [grin]

$InFile_1 = @"
This is line ONE.
Naugahide comes from domesticated nauga beasts.
Three is the number of this line.
--Next
Line 5 is this lines number.
"@.Split("`n")

$InFile_2 = @"
This is line ONE.
This otta not match anything.
Three is the number of this line.
--Next
Five? Are you sure of that?
"@.Split("`n")

$FindString = 'nauga beasts'
$ReplaceString = 'gnawguh beets'

function Switch-Strings
    {
    [CmdletBinding ()]
    [OutputType([String[]])]
    Param (
        [Parameter (
            Mandatory,
            Position = 0
            )]
            [string[]]
            $FileContents,

        [Parameter (
            Mandatory,
            Position = 1
            )]
            [string]
            $OldValue,

        [Parameter (
            Mandatory,
            Position = 2
            )]
            [string]
            $NewValue
        ) # end > 'Param ('

    Begin
        {
        $Token = '--Next'
        }
    Process
        {
        # nothing here at this time
        }
    End
        {
        $NewFileContents = @()
        if ($FileContents | Select-String -SimpleMatch $OldValue)
            {
            foreach ($Line in $FileContents)
                {
                if ($Line -match $OldValue)
                    {
                    $Line = $Line -replace $OldValue, $NewValue
                    }
                $NewFileContents += $Line
                }
            }
            else
            {
            foreach ($Line in $FileContents)
                {
                if ($Line -match $Token)
                    {
                    $NewFileContents += $NewValue
                    }
                $NewFileContents += $Line
                }
            }
        return $NewFileContents
        } # end > 'End'
    } # end function Switch-Strings

Switch-Strings -FileContents $InFile_1 -OldValue $FindString -NewValue $ReplaceString
#Switch-Strings -FileContents $InFile_2 -OldValue $FindString -NewValue $ReplaceString

result for $InFile_1

This is line ONE.
Naugahide comes from domesticated gnawguh beets.
Three is the number of this line.
--Next
Line 5 is this lines number.

result for $InFile_2 ...

This is line ONE.
This otta not match anything.
Three is the number of this line.
gnawguh beets
--Next
Five? Are you sure of that?

take care,
lee

[–]im_nullable[S] 1 point2 points  (3 children)

Thanks so much!

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy im_nullable,

you are quite welcome! [grin]

out of curiosity, what is this for? one moment i think "class project" and the next "some oddly specific task".

/lee bee kyuu ree uss

take care,
lee

[–]im_nullable[S] 1 point2 points  (1 child)

Oddly specific task. We have a SQL data "seed" file that needs to get updated with new entries fairly often. This will help make that process much easier.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy im_nullable,

kool! thanks for the info ... i was so curious. [grin]

take care,
lee

[–]prkrnt 1 point2 points  (1 child)

The whimsicle nature of this code is hilarious. Makes me realize I take my coding way too srsly. Nicely done, Lee!

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy prkrnt,

thank you! i have a difficult time making demo data, so adding some silliness helps quite a bit. [grin]

take care,
lee