嗨,我有一个带有日志的文件,这个日志的一些字段由空格分隔,另一些字段由一个或两个空格分隔,如何使用powershell将所有的空白空间设置为一个单独的空间?
类似于linux bash中的awk,因为我有一个字段在他的值中包含层叠:"type:Windows Resources“
awk '{print " $1 " " $2 " " $3 " " $5 " " $6 " " $7 " " $8 " " $9 " " $10}'发布于 2020-02-27 18:25:56
以下将只替换2个或更多的空间为单一空间。
((Get-Content logFile.txt) -replace '\s{2,}',' ') >> logFileTemp.txt;
Copy-Item logFileTemp.txt logFile.txt -Force发布于 2020-02-27 17:36:04
如下所示,替换一个或多个空白空间:
echo "hi hi`t`thi" > file1.txt
cat file1.txt
hi hi hi
(cat file1.txt) -replace '\s+',' ' | set-content file2.txt
cat file2.txt
hi hi hi发布于 2020-02-27 17:40:57
$test = " Testing removal of spaces"
$SpacesConverted = $test -replace "[ ]{1,1000}"," "
$SpacesConverted输出: Testing removal of spaces
我正在做的是替换空格,寻找介于1到1000之间的任何空间,并将其转换为单个空间。如果在日志的任何给定部分中有超过1000个空格,只需增加这个数目。
如果你想在文件上运行这个..。
$File = "c:\file123.txt"
gc $File -replace "[ ]{1,1000}"," " | sc $Filehttps://stackoverflow.com/questions/60437986
复制相似问题