Korn Shell 模式检测和删除
Korn Shell pattern detection and deletion
我正在与 Korn Shell 合作,尝试检测平面文件中不需要的错误消息。平面文件有多个错误记录,其中一些可以删除,另一些需要保留。错误如:
Message: TR0405 Severity: 04
Application data received will be ignored.
blah blah blah.
需要删除,同时需要保留带有任何其他代码的消息。我在删除这条记录时遇到了问题,同时保持下一条记录完整无缺。我想从检测到的模式中删除
Message: TR0405 Severity: 04
到刚刚检测到的下一个模式
Message:
我当前的代码:
sed -e "/Message: TR0405 Severity: 04/,/Message:/{//p;d;}" test.txt > newTest.txt
rm test.txt
mv newTest.txt test.txt
数据示例:
Message: TR0405 Severity: 04
Application data received will be ignored.
blah blah blah.
Message: TR0405 Severity: 04
Application data received will be ignored.
blah blah blah.
Message: TR0405 Severity: 04
Application data received will be ignored.
blah blah blah.
More blah blah blah.
Message: TR0425 Severity: 04
Error with timing.
blah blah blah.
像这样的东西怎么样:(已编辑以说明新示例)
#!/bin/ksh
Logfile=
while read line; do
if [[ $line == Message* ]]; then
flag=0
case $line in
*TR0405*) continue
;;
"") continue
;;
*) print $line >> logresult.txt
flag=1
;;
esac
elif [[ $flag -eq 1 ]]; then
print $line >> logresult.txt
fi
done < $Logfile
您可以在 case 语句中添加任何错误代码。使用您的数据示例效果很好。
我正在与 Korn Shell 合作,尝试检测平面文件中不需要的错误消息。平面文件有多个错误记录,其中一些可以删除,另一些需要保留。错误如:
Message: TR0405 Severity: 04 Application data received will be ignored. blah blah blah.
需要删除,同时需要保留带有任何其他代码的消息。我在删除这条记录时遇到了问题,同时保持下一条记录完整无缺。我想从检测到的模式中删除
Message: TR0405 Severity: 04
到刚刚检测到的下一个模式
Message:
我当前的代码:
sed -e "/Message: TR0405 Severity: 04/,/Message:/{//p;d;}" test.txt > newTest.txt
rm test.txt
mv newTest.txt test.txt
数据示例:
Message: TR0405 Severity: 04
Application data received will be ignored.
blah blah blah.
Message: TR0405 Severity: 04
Application data received will be ignored.
blah blah blah.
Message: TR0405 Severity: 04
Application data received will be ignored.
blah blah blah.
More blah blah blah.
Message: TR0425 Severity: 04
Error with timing.
blah blah blah.
像这样的东西怎么样:(已编辑以说明新示例)
#!/bin/ksh
Logfile=
while read line; do
if [[ $line == Message* ]]; then
flag=0
case $line in
*TR0405*) continue
;;
"") continue
;;
*) print $line >> logresult.txt
flag=1
;;
esac
elif [[ $flag -eq 1 ]]; then
print $line >> logresult.txt
fi
done < $Logfile
您可以在 case 语句中添加任何错误代码。使用您的数据示例效果很好。