修改没有临时文件的文件的内容

时间:2012-08-20 08:25:56

标签: bash shell

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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

在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