bhavs
bhavs

Reputation: 2301

modify the contents of a file without a temp file

I have the following log file which contains lines like this

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 

I am trying extract the first field from each line and depending on whether it belongs to blah@13 or blah@14, blah@15 i am creating the corresponding files using the following script, which seems quite in-efficient in terms of the number of temp files creates. Any suggestions on how I can optimize it ?

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

Upvotes: 0

Views: 169

Answers (2)

James Gawron
James Gawron

Reputation: 969

I've found it useful at times to use ex instead of grep/sed to modify text files in place without using temps ... saves the trouble of worrying about uniqueness and writability to the temp file and its directory etc. Plus it just seemed cleaner.

In ksh I would use a code block with the edit commands and just pipe that into 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

Upvotes: 1

Stephane Rouberol
Stephane Rouberol

Reputation: 4384

Something like this ?

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

Upvotes: 2

Related Questions