Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace the content of a textfile with a regex in powershell

I have a simple textfile and I need a powershell script to replace some parts of the file content.

My current script is the following:

$content = Get-Content -path "Input.json"

$content -Replace '"(\d+),(\d{1,})"', '$1.$2' |  Out-File "output.json"

Is it possible to write it in one line without the content variable, like this?

Get-Content -path "Input.json" | ??? -Replace '"(\d+),(\d{1,})"', '$1.$2' |  Out-File "output.json"

I don't know how I can use the output of the first get-content commandlet in the second command without the $content variable? Is there an automatic powershell variable

Is it possible to do more replacements than one in a pipeline.

Get-Content -path "Input.json" | ??? -Replace '"(\d+),(\d{1,})"', '$1.$2' | ??? -Replace 'second regex', 'second replacement' |  Out-File "output.json"
like image 329
Jan Baer Avatar asked Apr 27 '13 09:04

Jan Baer


People also ask

How do I replace text in a text file in PowerShell?

Use Get-Content and Set-Content to Replace Every Occurrence of a String in a File With PowerShell. The Get-Content gets the item's content in the specified path, such as the text in a file. The Set-Content is a string-processing cmdlet that allows you to write new content or replace the existing content in a file.

Can I use regex in replace?

How to use RegEx with . replace in JavaScript. To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring.

How do you use regex expressions in PowerShell?

A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data. Matches the beginning of the line.


2 Answers

Yes, you can do that in one line and don't even need a pipeline, as -replace works on arrays like you would expect it to do (and you can chain the operator):

(Get-Content Input.json) `
    -replace '"(\d+),(\d{1,})"', '$1.$2' `
    -replace 'second regex', 'second replacement' |
  Out-File output.json

(Line breaks added for readability.)

The parentheses around the Get-Content call are necessary to prevent the -replace operator being interpreted as an argument to Get-Content.

like image 173
Joey Avatar answered Oct 18 '22 19:10

Joey


Is it possible to write it in one line without the content variable, like this?

Yes: use ForEach-Object (or its alias %) and then $_ to reference the object on the pipeline:

Get-Content -path "Input.json" | % { $_ -Replace '"(\d+),(\d{1,})"', '$1.$2' } |  Out-File "output.json"

Is it possible to do more replacements than one in a pipeline.

Yes.

  1. As above: just adding more Foreach-Object segments.
  2. As -replace returns the result, they can be chained in a single expression:

    ($_ -replace $a,$b) -replace $c,$d
    

    I suspect the parentheses are not needed, but I think they make it easier to read: clearly more than a few chained operators (especially if the match/replacements are non-trivial) will not be clear.

like image 14
Richard Avatar answered Oct 18 '22 20:10

Richard