我有许多命令都可以独立运行。下面的5个awk命令当前输出所需的数据,以逗号分隔。我想将所有下面的awk输出结合起来,这样它们就会出现在一个单独的csv文件中,但我没有太多运气。
以下命令只搜索包含特定单词的行,并提取所需字段的输出。例如..我希望$ 4,$ 5,$ 6,$ 7,$ 8,$ 9从第一个命令填充前6列,然后在接下来的2列中从第二个命令输出$ 2,$ 4。
awk '/start/ { print $4,$5,$6,$7,$8,$9 }' 07.08.2014.txt | sed -e "s/ /,/g"
awk '/PacketLoss/ { print $2,$4 }' 07.08.2014.txt | sed -e "s/ /,/g"
awk '/PacketOutOfSequence/ { print $2,$4,$6 }' 07.08.2014.txt | sed -e "s/ /,/g"
awk '/JitterSD/ { print $3,$6,$9 }' 07.08.2014.txt | sed -e "s/ /,/g"
awk '/NumOfRTT/ { print $2,$4,$6,$8 }' 07.08.2014.txt | sed -e "s/ /,/g"
示例输入数据
Aggregation start time 11:45:47.893 BST Thu Aug 7 2014
NumOfRTT: 360000 RTTAvg: 145 RTTMin: 144 RTTMax: 171
PacketLossSD: 0 PacketLossDS: 0
PacketOutOfSequence: 3 PacketMIA: 0 PacketLateArrival: 0
Jitter Avg: 1 JitterSD Avg: 1 JitterDS Avg: 1
示例输出
11:45:47.893,BST,Thu,Aug,7,2014,0,0,3,0,0,1,1,1,360000,145,144,171
如下所示标记每一列也不错,但如果我手动操作太复杂,那就不重要了
START_TIME,BST,DAY,MONTH,DATE,YEAR,PacketLossSD,PacketLossDS,PacketOutOfSeq,PacketMIA,PacketLateArrival,JitterAvg,JitterSD_Avg,JitterSD_Avg,NumOfRTT,RTTAvg,RTTMin,RTTMax
11:45:47.893,BST,Thu,Aug,7,2014,0,0,3,0,0,1,1,1,360000,145,144,171
提前感谢您的任何帮助:)
答案 0 :(得分:4)
awk '
NR==1 { print "START_TIME,BST,DAY,MONTH,DATE,YEAR,PacketLossSD,PacketLossDS,PacketOutOfSeq,PacketMIA,PacketLateArrival,JitterAvg,JitterSD_Avg,JitterSD_Avg,NumOfRTT,RTTAvg,RTTMin,RTTMax" }
/start/ { tm = sprintf("%s,%s,%s,%s,%s,%s",$4,$5,$6,$7,$8,$9) }
/PacketLoss/ { pl = sprintf("%s,%s",$2,$4) }
/PacketOutOfSequence/ { pos = sprintf("%s,%s,%s",$2,$4,$6) }
/NumOfRTT/ { rtt = sprintf("%s,%s,%s,%s",$2,$4,$6,$8) }
/JitterSD/ { printf("%s,%s,%s,%s,%s,%s,%s\n",tm,pl,pos,$3,$6,$9,rtt) }
' 07.08.2014.txt
这个想法是在读取行时将数据保存在字符串中,并且只在读取最后一行(假设为包含" JitterSD"的行)时将其打印出来。
替代想法:
awk '
BEGIN { RS=""; FS="\n"; OFS="," }
{
split($1,a," "); L1=a[4]","a[5]","a[6]","a[7]","a[8]","a[9]
split($2,a," "); L2=a[2]","a[4]","a[6]","a[8]
split($3,a," "); L3=a[2]","a[4]
split($4,a," "); L4=a[2]","a[4]","a[6]
split($5,a," "); L5=a[3]","a[6]","a[9]
print L1, L3, L4, L5, L2
}
' 07.08.2014.txt
RS=""
是一种特殊设置,可将记录拆分为一行或多行,用于多行记录。
FS="\n"
会将$1
,$2
等设置为多行记录的第1行,第2行等。
split($1,a," ")
将行拆分为以空格分隔的字段,将它们放入数组a
中。