并行化 for 循环来解析文件

并行化 for 循环来解析文件

我有以下脚本来解析一些pcap跟踪文件。有多个文件夹,每个文件夹包含4个pcap需要解析的文件:

for d in * ; 
do cd ${d}; 
for file in firewall router2 vclient vserver ; 
do tshark -r ${file}_log.pcap -T fields -e ip.src -e ip.dst -e tcp.srcport -e tcp.dstport -e tcp.len -e ip.len -e ip.hdr_len -e tcp.hdr_len -e _ws.col.Time -t e | awk '{print $1,$2,$3,$4,$6-$7-$8,$9}' > ${file}_conv_t.txt; 
cat ${file}_conv_t.txt | awk '$4==5001{print $6,$5}' | sed 's/\([0-9]*\)\.[0-9]*/\1/' | awk '{sum[$1]+=$2} END {for (i in sum) {print i,sum[i]} }' | sort | cut -d ' ' -f 2 > ${file}_in.csv; 
cat ${file}_conv_t.txt | awk '$3==5001{print $6,$5}' | sed 's/\([0-9]*\)\.[0-9]*/\1/' | awk '{sum[$1]+=$2} END {for (i in sum) {print i,sum[i]} }' | sort | cut -d ' ' -f 2 > ${file}_out.csv ; 
done ; 
cd .. ; 
done

由于所有文件都可以独立解析,因此我想尽可能并行化。我尝试在第一个文件之后放置 a&而不是,以便并行处理每个文件,如下所示:;done

for d in * ; 
do cd ${d}; 
for file in firewall router2 vclient vserver ; 
do tshark -r ${file}_log.pcap -T fields -e ip.src -e ip.dst -e tcp.srcport -e tcp.dstport -e tcp.len -e ip.len -e ip.hdr_len -e tcp.hdr_len -e _ws.col.Time -t e | awk '{print $1,$2,$3,$4,$6-$7-$8,$9}' > ${file}_conv_t.txt; 
cat ${file}_conv_t.txt | awk '$4==5001{print $6,$5}' | sed 's/\([0-9]*\)\.[0-9]*/\1/' | awk '{sum[$1]+=$2} END {for (i in sum) {print i,sum[i]} }' | sort | cut -d ' ' -f 2 > ${file}_in.csv; 
cat ${file}_conv_t.txt | awk '$3==5001{print $6,$5}' | sed 's/\([0-9]*\)\.[0-9]*/\1/' | awk '{sum[$1]+=$2} END {for (i in sum) {print i,sum[i]} }' | sort | cut -d ' ' -f 2 > ${file}_out.csv ; 
done &
cd .. ; 
done

但这根本不起作用,它仍然是顺序的。

我缺少什么?

答案1

使用{}大括号:

for d in * ; 
do cd ${d}; 
for file in firewall router2 vclient vserver ; 
do { tshark -r ${file}_log.pcap -T fields -e ip.src -e ip.dst -e tcp.srcport -e tcp.dstport -e tcp.len -e ip.len -e ip.hdr_len -e tcp.hdr_len -e _ws.col.Time -t e | awk '{print $1,$2,$3,$4,$6-$7-$8,$9}' > ${file}_conv_t.txt; 
cat ${file}_conv_t.txt | awk '$4==5001{print $6,$5}' | sed 's/\([0-9]*\)\.[0-9]*/\1/' | awk '{sum[$1]+=$2} END {for (i in sum) {print i,sum[i]} }' | sort | cut -d ' ' -f 2 > ${file}_in.csv; 
cat ${file}_conv_t.txt | awk '$3==5001{print $6,$5}' | sed 's/\([0-9]*\)\.[0-9]*/\1/' | awk '{sum[$1]+=$2} END {for (i in sum) {print i,sum[i]} }' | sort | cut -d ' ' -f 2 > ${file}_out.csv ; } &
done
cd .. ; 
done

相关内容