# How to replace a string for a specific line number

**URL:** <https://forums.powershell.org/t/how-to-replace-a-string-for-a-specific-line-number/15447>\
**Category:** PowerShell Help\
**Created:** [December 31, 2020, 4:52am UTC](https://forums.powershell.org/t/how-to-replace-a-string-for-a-specific-line-number/15447 "2020-12-31T04:52:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![scottich-kosmos](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@scottich-kosmos](https://forums.powershell.org/u/scottich-kosmos)\
**Post date:** [December 31, 2020, 4:52am UTC](https://forums.powershell.org/t/how-to-replace-a-string-for-a-specific-line-number/15447/1 "2020-12-31T04:52:50Z")

</div>

I have few SQL script files and I am trying to remove the “GO” keyword from the files. unfortunately, there are many other words matching the search criteria and I do not want to replace them.

I want to replace the “GO” word only in those lines wherein the line does not have any other words except “GO”. I am able to find the line numbers where I have to do the changes but not sure how to make the changes in files.

&nbsp;

$FILEFOLDER =gc “c:\test.sql”

$linenumber= $FILEFOLDER | select-string “GO”

foreach($line in $linenumber) {  
$linenumbers=$line.LineNumber  
$test1=$line.Line.Trim()  
$len=$test1.length  
if($len -eq 2)  
{

$contentUpdate = $FILEFOLDER[$linenumbers] -replace “GO=”,“GO=$output”  
Set-Content $NEWPATH $contentUpdate

}

}

&nbsp;

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [December 31, 2020, 5:16am UTC](https://forums.powershell.org/t/how-to-replace-a-string-for-a-specific-line-number/15447/2 "2020-12-31T05:16:08Z")

</div>

```
# remove the “GO” keyword from the file

# Create Sample file
@'
GO
Go places
Let's GO
Go
'@ | Out-File .\mySQL1.txt

# Process
Get-Content .\mySQL1.txt | foreach {
    if ($_.ToLower() -ne 'go') { $_ } # remove lines that are 'go'
} | Out-File .\mySQL2.txt

# Validate
notepad .\mySQL1.txt
notepad .\mySQL2.txt
```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [December 31, 2020, 6:47am UTC](https://forums.powershell.org/t/how-to-replace-a-string-for-a-specific-line-number/15447/3 "2020-12-31T06:47:18Z")

</div>

Another method using Select-String. Here we use a regular expression with the start of string (^) and end of string ($) anchors to get an exact match on the line. We output all lines that don’t match to another file.

&nbsp;

```
# Create Sample file
@'
GO
Go places
Let's GO
Go
'@ | Out-File .\mySQL1.txt

(Select-String -Path .\mySQL1.txt -Pattern '^go$' -NotMatch).Line | Set-Content .\mySQL2.txt
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/how-to-replace-a-string-for-a-specific-line-number/15447/4 "2024-05-16T20:28:06Z")

</div>

Closed on May 16, 2024, 8:28pm UTC


