0

我有以下日志文​​件,其中包含这样的行

1345447800561|FINE|blah@13|txReq
1345447800561|FINE|blah@13|Req
1345447800561|FINE|blah@13|rxReq
1345447800561|FINE|blah@14|txReq
1345447800561|FINE|blah@15|Req 

我正在尝试从每一行中提取第一个字段,并根据它属于 blah@13 还是 blah@14,blah@15 我正在使用以下脚本创建相应的文件,这在数量方面似乎效率很低的临时文件创建。关于如何优化它的任何建议?

cat newLog | grep -i "org.arl.unet.maca.blah@13" >> maca13
cat newLog | grep -i "org.arl.unet.maca.blah@14" >> maca14
cat newLog | grep -i "org.arl.unet.maca.blah@15" >> maca15
 cat maca10 | grep -i "txReq" >> maca10TxFrameNtf_temp
exec<blah10TxFrameNtf_temp
while read line 
do
 echo $line | cut -d '|' -f 1 >>maca10TxFrameNtf
done
cat maca10 | grep -i "Req" >> maca10RxFrameNtf_temp
 while read line 
do
 echo $line | cut -d '|' -f 1 >>maca10TxFrameNtf
done
rm -rf *_temp
4

2 回答 2

2

像这样的东西?

for m in org.arl.unet.maca.blah@13 org.arl.unet.maca.blah@14 org.arl.unet.maca.blah@15
do
  grep -i "$m" newLog | grep "txReq" | cut -d' ' -f1 > log.$m
done
于 2012-08-20T08:40:33.937 回答
1

我发现有时使用 ex 而不是 grep/sed 来修改文本文件而不使用临时文件很有用......省去了担心临时文件及其目录等的唯一性和可写性的麻烦。另外它看起来清洁器。

在 ksh 中,我将使用带有编辑命令的代码块,然后将其通过管道传输到 ex ...

{
  # Any edit command that would work at the colon prompt of a vi editor will work
  # This one was just a text substitution that would replace all contents of the line
  # at line number ${NUMBER} with the word DATABASE ... which strangely enough was
  # necessary at one time lol
  # The wq is the "write/quit" command as you would enter it at the vi colon prompt
  # which are essentially ex commands.
  print "${NUMBER}s/.*/DATABASE/"
  print "wq"
} | ex filename > /dev/null 2>&1
于 2013-12-17T22:56:12.537 回答