我试图在PowerShell中过滤我的文件,因此它只包含有价值的行。有没有办法确定[CAR]标签后面是否有值?
或者我必须写这样的东西吗?
$file = Get-Content C:\file.txt
foreach ($line in $file){
if ($line.Length -gt 47){
#somehow safe those lines in another file
}
}
示例原始文件:
25 13:58:09.680 (0410041011) >>> DIAG[CAR]
25 13:58:09.788 (0410041120) <<< [NAK]#99[CAR]0998034398[CAR]0998028069[CAR]
25 13:58:29.789 (0410061120) >>> DIAG[CAR]
25 13:58:29.896 (0410061227) <<< [NAK]#99[CAR]0998023613[CAR]
25 13:58:49.897 (0410081228) >>> DIAG[CAR]
25 13:58:49.981 (0410081311) <<< [NAK]#99[CAR]0998007552[CAR]0998019114[CAR]0998007115[CAR]
25 13:59:09.982 (0410101312) >>> DIAG[CAR]
25 13:59:10.069 (0410101399) <<< [NAK]#99[CAR]0998024143[CAR]
25 13:59:29.070 (0410120400) >>> DIAG[CAR]
25 13:59:29.177 (0410120507) <<< [NAK]#99[CAR]
应该在新文件中的内容示例:
25 13:58:09.788 (0410041120) <<< [NAK]#99[CAR]0998034398[CAR]0998028069[CAR]
25 13:58:29.896 (0410061227) <<< [NAK]#99[CAR]0998023613[CAR]
25 13:58:49.981 (0410081311) <<< [NAK]#99[CAR]0998007552[CAR]0998019114[CAR]0998007115[CAR]
25 13:59:10.069 (0410101399) <<< [NAK]#99[CAR]0998024143[CAR]
答案 0 :(得分:1)
$data = @(
"25 13:58:09.680 (0410041011) >>> DIAG[CAR]",
"25 13:58:09.788 (0410041120) <<< [NAK]#99[CAR]0998034398[CAR]0998028069[CAR]",
"25 13:58:29.789 (0410061120) >>> DIAG[CAR]",
"25 13:58:29.896 (0410061227) <<< [NAK]#99[CAR]0998023613[CAR]",
"25 13:58:49.897 (0410081228) >>> DIAG[CAR]",
"25 13:58:49.981 (0410081311) <<< [NAK]#99[CAR]0998007552[CAR]0998019114[CAR]0998007115[CAR]",
"25 13:59:09.982 (0410101312) >>> DIAG[CAR]",
"25 13:59:10.069 (0410101399) <<< [NAK]#99[CAR]0998024143[CAR]",
"25 13:59:29.070 (0410120400) >>> DIAG[CAR]",
"25 13:59:29.177 (0410120507) <<< [NAK]#99[CAR]"
)
# Use lookahead for a digit
$data -match "\[CAR\](?=\d)"