time performance enhancment in bash script

2019-08-07 10:02发布

I have different text files and different outputs from different sources. I need to have the output on different lines on a text document file. The code is as follows

for (( x = 1 ; x < 1000 ; x++ )) do

difference=$((file1 - file2))
echo $(tshark -r 1.pcap -c 1 -t ad | \
        awk -F" " '{print $2,$3}')  $difference \
        $(awk 'FNR == "'$z'" {print}' 1.txt) >> ~/Desktop/information.txt
done

It works fine, but it takes a long time. I beleive it is because the script is accessing the text document many times in a loop. Any idea how to use other approaches to enhance the speed performance?

Thanks

标签: linux bash
1条回答
▲ chillily
2楼-- · 2019-08-07 10:19

You can start with taking the redirection out of the loop:

for (( x = 1 ; x < 1000 ; x++ )) do
  difference=$((file1 - file2))
  echo $(tshark -r 1.pcap -c 1 -t ad | \
        awk -F" " '{print $2,$3}')  $difference \
        $(awk 'FNR == "'$z'" {print}' 1.txt)
done >> ~/Desktop/information.txt
查看更多
登录 后发表回答