Reputation: 53
I am very new to scripting. I am trying to solve one problem and I was looking at windows batch and powershell. I have found this but I dont know, why it is not working:
I have input.txt file with this content:
<a> TEXT </a>
<c> text asdrtlj </c>
<a> another text </a>
I want to add new line before every <a>
, so I tried this:
powershell -Command "(gc input.txt) -replace '<a>', '`r`n<a>' | Out-File output.txt"
That adding of newline doesnt work. Can you help me please?
PS: I've found a lot of complicated codes while searching the solution and I haven't understood them yet so if you recommend me some good tutorials where to start with this languages I will appreciate it.
Upvotes: 1
Views: 10356
Reputation: 3429
We need more quotation marks.
powershell.exe -command "(gc 1_105.jpg) -replace '<a>', """"`r`n<a>""""|Out-File output.txt"
Or how is about a small refactoring?
powershell.exe -Command "gc input.txt|%{$_.replace('<a>',[environment]::newline+'<a>')}|Out-File output.txt"
Upvotes: 2
Reputation: 23355
There are a couple of issues here. Executing a command via the -command
switch of powershell is causing some reserved character issues due to the < > characters.
Instead, open a PowerShell.exe window (start -> run -> powershell) and execute the command directly.
You also need to use double quotes not single quotes in order for special codes like `r and `n to work. Also `n should be sufficient on its own:
(gc input.txt) -replace '<a>', "`n<a>" | Out-File output.txt
Upvotes: 5