whomaniac
whomaniac

Reputation: 1318

change line in file

I am trying to write a PowerShell script that does the following:

  1. Open a text file.

  2. Find a line that matches the following pattern:

    public interface $A extends InterfaceName
    

    and change it to:

    public interface $A extends InterfaceName<$A>
    
  3. Save the file.

Upvotes: 0

Views: 304

Answers (1)

krivtom
krivtom

Reputation: 24916

You can read the data from a file using Get-Content, search for the match and replace it using Foreach-Object and -replace, and then write it back to the file using Set-Content. The regular expression can be used to add the code that you need:

# Get the content from a file
Get-Content 'C:\path_to_your_file.txt' | 
# Get replace each line if it matches the pattern
ForEach-Object {$_ -replace "(public interface (\S+) extends InterfaceName)", ' $1<$2>' } |
# Save the changes back to the file
Set-Content 'C:\path_to_your_file.txt'

Upvotes: 2

Related Questions