Home > Enterprise >  How to add and remove Lines Powershell Script
How to add and remove Lines Powershell Script

Time:08-31

this is my script:

foreach ($Prog in $input) {
    (Get-Content $Prog.CamPath) | Foreach-Object {
            $_ # send the current line to output
                 if ($_ -match '!')  {  
                '<139 \Komponente\'
                'IN="4Seiten_fuegen.mpr"'
                'XA="0.0000"'
                'YA="0.0000"'
                'ZA="0.0000"'
                'EM="0"'
                'PR="0"'
                'VA="length 500.0000"'
                'VA="width 500.0000"'
                'VA="rotation 0.0000"'
                'KO="00"'
                '!'
        }
    } | Set-Content $Prog.CamPath
}

It adds those Lines beneth the "!"

The Result ist something like that:

!
<139 \Komponente\
IN="4Seiten_fuegen.mpr"
XA="0.0000"
YA="0.0000"
ZA="0.0000"
EM="0"
PR="0"
VA="length 500.0000"
VA="width 500.0000"
VA="rotation 0.0000"
KO="00"
!

It works all fine. I just need to remove the first "!" Ive googled for hours! nothing works.

Could anybody just tell me how to modify my script? Probably something like

-remove ("!","")
$_remove ('!','')

I dont know where and how....

kind regards

CodePudding user response:

Change your code to only output $_ after you've tested whether it contains !:

... |ForEach-Object {
    if ($_ -match '!')  {  
        '<139 \Komponente\'
        'IN="4Seiten_fuegen.mpr"'
        'XA="0.0000"'
        'YA="0.0000"'
        'ZA="0.0000"'
        'EM="0"'
        'PR="0"'
        'VA="length 500.0000"'
        'VA="width 500.0000"'
        'VA="rotation 0.0000"'
        'KO="00"'
    }
    $_
}
  • Related