#sed -i '/wavetcp_init/,$!d' file.txt
#rm *.dat *.agr;
time=$(cat file.txt | grep "wavetcp_init" | awk 'NR==1{print $2}');
echo time0 = $time;

sport=$(cat file.txt | grep "wavetcp_init" | awk '{print $8}');
echo S_Ports = $sport;
for i in $sport; do
echo i = $i;
cat file.txt | awk -v sport=$i '$8==sport {print($0)}' > log_$i.dat;

cat log_$i.dat | grep "tcp_transmit_skb] seq" | awk -v t=$time 'NR==1{seq=$11} NR>1{print(($2- t), ($11-seq-1), ($17-66), 0)}' >> seq.dat;

cat log_$i.dat | grep "returning timer of" | awk -v t=$time '{print(($2- t), $13/1000, $8)}' > tx_time_$i_$i.dat;
cat log_$i.dat | grep "wavetcp_round_terminated] delta" | awk -v t=$time '{print(($2- t), $12/1000, $8)}' > delta_rtt_$i_$i.dat;
cat log_$i.dat | grep "final ack_train_disp" | awk -v t=$time '{print(($2- t), $15/1000)}' > ack_train_disp_$i.dat;
cat log_$i.dat | grep "using measured" | awk -v t=$time '{print(($2- t), $13/1000)}' > measured_ack_train_disp_$i.dat;
cat log_$i.dat | grep "alpha" | awk -v t=$time '{print(($2- t), $17/$11)}' > alpha_$i.dat;
cat log_$i.dat | grep "rcv_ack_train_disp" | awk -v t=$time '{print(($2- t), $19/1000)}' > rec_ack_train_disp_$i.dat;
cat log_$i.dat | grep "filtered_ack_train_disp" | awk -v t=$time '{print(($2- t), $11/1000)}' > filtered_ack_disp_$i.dat;
cat log_$i.dat | grep "calculate_delta_rtt] ini" | awk -v t=$time '{print(($2- t), $15/1000)}' > rtt2_$i.dat;
cat log_$i.dat | grep "calculate_delta_rtt] final" | awk -v t=$time '{print(($2- t), $12/1000)}' > avg_rtt_$i.dat;
cat log_$i.dat | grep "calculate_delta_rtt] ini" | awk -v t=$time '{print(($2- t), $18/1000)}' > min_rtt_$i.dat;
cat log_$i.dat | grep "wavetcp_acked] pkts" | awk -v t=$time '{print(($2- t), $13/1000)}' > rtt_us_$i.dat;
cat log_$i.dat | grep "reached" | awk -v t=$time '{print(($2- t), $14)}' > burst_size_$i.dat;
cat log_$i.dat | grep "wavetcp_acked] pkts" | awk -v t=$time 'NR==1{ack=$21}NR>1{$3=$21-ack-prev21; prev21= $21-ack}NR>1{print(($2- t), ($21-ack), -($3), 0)}' >> ack.dat;

xmgrace rtt2_*.dat avg_rtt_*.dat min_rtt_*.dat delta_rtt_*.dat rtt_us_*.dat -saveall rtts_all.agr -hardcopy -noprint;
xmgrace -settype xydydy seq.dat -settype xydydy ack.dat -saveall seq_ack.agr -hardcopy -noprint;
xmgrace ack_train_disp_*.dat measured_ack_train_disp_*.dat -saveall ack_disp.agr -hardcopy -noprint;
xmgrace rec_ack_train_disp_*.dat filtered_ack_disp_*.dat -saveall alfa_output.agr -hardcopy -noprint;
xmgrace ack_train_disp_*.dat tx_time_*.dat delta_rtt_*.dat -saveall tx_timer.agr -hardcopy -noprint;

done
exit
