在powershell中有类似于awk的命令吗?
我想执行以下命令:
awk '
BEGIN {count=1}
/^Text/{text=$0}
/^Time/{time=$0}
/^Rerayzs/{retext=$0}
{
if (NR % 3 == 0) {
printf("%s\n%s\n%s\n", text, time, retext) > (count ".txt")
count++
}
}' file到powershell命令。
发布于 2014-04-22 23:23:37
通常我们喜欢看你试过的东西。这至少表明你在努力,我们不只是在为你做你的工作。我认为你是PowerShell的新手,所以我只想给你一个答案,希望你能用它来学习和扩展你的知识,并希望将来有更好的问题。
我非常肯定,这将完成与你所做的同样的事情。您必须给它一个输入数组(文本文件的内容、字符串数组之类的内容),它将生成几个文件,具体取决于它为treo " text“、"Time”和"Rerayzs“找到了多少匹配。它会将它们排序为文本,然后是随时间变化的新行,然后是Rerayzs的新行。
$Text,$Time,$Retext = $Null
$FileCounter = 1
gc c:\temp\test.txt|%{
Switch($_){
{$_ -match "^Text"} {$Text = $_}
{$_ -match "^Time"} {$Time = $_}
{$_ -match "^Rerayzs"} {$Retext = $_}
}
If($Text -and $Time -and $Retext){
("{0}`n{1}`n{2}") -f $Text,$Time,$Retext > "c:\temp\$FileCounter.txt"
$FileCounter++
$Text,$Time,$Retext = $Null
}
}它将获取文件C:\Temp\Test.txt的文本,并将编号文件输出到相同的位置。我测试的文件是:
Text is good.
Rerayzs initiated.
Stuff to not include
Time is 18:36:12
Time is 20:21:22
Text is completed.
Rerayzs failed.我留下了两个文件作为输出。第一条规定:
Text is good.
Time is 18:36:12
Rerayzs initiated.第二条规定:
Text is completed.
Time is 20:21:22
Rerayzs failed.https://stackoverflow.com/questions/23230495
复制相似问题