Powershell ver 4. Windows 7
I wanted to replace , with new lines in a text file. I tried the script below
(Get-Content C:\Test\test.txt).Replace(',','`n') | Set-Content C:\Test\testv2.txt
but when I see the output file I see , replaced with '`n' instead of new line.
I also tried double quotes instead of single.
Replace(',',"`n")
This worked for me.
(Get-Content C:\Test\Test.txt) -replace ',',"`n" | Set-Content C:\Test\Test.txt -Force
Try this:
[IO.File]::ReadAllText(C:\Test\test.txt) -replace ',',"`r`n" | Out-File C:\Test\testv2.txt
P.S. Sorry that don't have time to explain it, now.
Related
I need to replace some strings multiple text files. I am running into an issue with the following string though. I tried escaping with a backtick ` before any special characters, but no luck, or maybe the backtick isn't in the right place.
I want to replace
Signal\s+:\s+',''
with
Signal\s+:\s+','';if (!`$signal) {`$signal='n/a'}
This is the string in the command that i am having a problem finding --> Signal\s+:\s+',''
Here is the powershell command i am using..
Get-ChildItem "f:\temp\*.ps1" | ForEach-Object -Process {(Get-Content $_) -Replace "Signal\s+:\s+',''" , "Signal\s+:\s+','';if (!`$signal) {`$signal='n/a'}" | Set-Content $_}
thank you
i just needed to escape the \ in the replace text.. thats all
-Replace "Signal\\s\+:\\s\+',''"
thanks
I have tried a lot, but I cannot seem to replace the new line at the start of a txt file.
So my txt file looks like this:
I just want to remove the first newline character, but everything I try does not work:
Replace ``n`r, replace \n\r or any combination of these.
Try
(Get-Content -Path 'YourFile.txt' -Raw).TrimStart() | Set-Content -Path 'YourFile.txt' -Force
Or
(Get-Content -Path 'YourFile.txt' -Raw) -replace '^\s+' | Set-Content -Path 'YourFile.txt' -Force
Explanation:
The above removes all whitespace (tabs, spaces, newlines) from the top of the text, as it is impossible to see from the image if other whitespace characters are in that line or not.
If you are sure there is just the one newline, in your case \r\n won't work, because the file uses Unix newlines (\n only).
Better is to replace using ^\r?\n. The ^ anchors at the beginning of the text. The ? reads zero or one on the CR character \r
This would replace all blank lines. The parentheses make sure the first command finishes first if you're writing to the same file.
(get-content file.txt) | where { $_ } | set-content file.txt
Or this way, the filename goes first.
set-content file.txt (get-content file.txt).where{$_}
A different approach
( Get-Content -Tail ( ( Get-Content testfile1 ).count-1 ) testfile1 ) | Set-Content testfile1
Count the number of lines in the file and then take one off the total. Use that to tail the file and write the output back to the file.
If you are certain that there will be an empty line (or you want to ignore it) you can use Skip.
Get-Content -Path 'YourFile.txt' | select -Skip 1 | Set-Content -Path 'YourFile.txt' -Force
I'm running PowerShell v4 and I'm trying to read in a multi-line text file that I want as a single string without line breaks. However, even when I use the -Raw parameter, it is still coming in as an array. It seems like after I put in the -Path the -Raw parameter isn't available anymore because I can't use tab completion for it.
I know there are ways to work around this issue. I'm wondering if anyone has insight into why it isn't working as expected.
Below command works for me:
((Get-Content $file) -join "`n") + "`n" | Set-Content -NoNewline $file
Works for PS v5
Using powershell, I want to take a .bat file and replace all lines in which set is called to set an environment variable and change it to a corresponding setx call. Unfortunately it's not as simple as just doing a search and replace on the file, replacing set for setx, because the syntax is diffferent: set ENVNAME=abc vs setx ENVNAME abc.
Is there a simple way to do this in powershell? To just do the set for setx replacement, I have:
(Get-Content $orig_filename ) | ForEach-Object {$_ -replace "set", "setx"} | Set-Content $new_filename
Any pointers for a powershell novice would be appreciated.
Did some pretty limited testing and I'm not sure what your bat file looks like, but this may work for you.
(Get-Content $orig_filename ) | ForEach-Object {
$_ -replace 'set (.*)\=(.*)','setx $1 $2'
} | Set-Content $new_filename
You could also just chain another -replace to do the work for you. Also made the set > setx replacement a little more less error prone by ensuring you are replacing the word set at the beginning of the string. Since -replace functions as an array operator you do not need the foreach loop.
(Get-Content $orig_filename ) -replace "^set\b", "setx" -replace "="," " | Set-Content $new_filename
I have a text file containing names of people seperated by . how to replace the . with new lines so that each name is in new line with powershell
If you have the latest version of PSCX (3.2.0) http://pscx.codeplex.com, we just added a new command to simplify this type of task a bit:
Edit-File -Path c:\path\to\file.txt -Pattern '\.' -Replacement "`r`n"
Or using positional params:
Edit-File c:\path\to\file.txt '\.' "`r`n"
This command also handles taking care that the file's original encoding is preserved. Using Out-File will output using Unicode unless you override with the -Encoding parameter which of course requires that you know the file's encoding in the first place. :-)
You can do a simple replace...
(gc c:\path\to\file.txt) -replace "\.","`n" | out-file c:\path\to\newfile.txt