假设我的输入文件是表分隔的,我如何使用AWK获取类名和可执行时间

时间:2013-07-25 11:40:32

标签: shell awk

假设我的输入文件是以表格分隔的,如何获取类名和可执行时间?

 - class Apple.java executed by user ABC34 executable time 980.9 ms
 - class ABC.java executed by user ABC34  executable time is in sec 45.9 ss
 - class TAS.java executed by user ABC34  executable time 75.9 ms
 - class GW.java executed by user ABC34  executable time is in sec 0.9 ss
 - class MYClass.java executed by user ABC34  executable time  is in sec 7.9 ss
 - class Times.java executed by user ABC34  executable time  is in sec 9.9 ss
 - class Sunday.java executed by user ABC34  executable time  is in sec 85.9 ss
 - class Week.java executed by user ABC34  executable time  is in sec 1000.9 ss

我想要像这样的外出

 - Apple.java 980.9 ms
 - ABC.java  45.9 ss
 - TAS.java  75.9 ms
 - GW.java  0.9 ss
 - MYClass.java 7.9 ss
 - Times.java  9.9 ss
 - Sunday.java 85.9 ss
 - Week.java  1000.9 ss

请看我的代码。我无法摆脱

#!/bin/bash

LOGFILE=$1
SEARCH=$2

echo $SEARCH

if [ -f $LOGFILE ];
then

awk '/ABC34 / && /is in sec/ {print $2, $13, $14;}' $LOGFILE>Output.txt
awk '/ABC34 / {print $2, $8, $9;}' $LOGFILE>Output.csv

else
echo "No File Found"
fi

但是当我检查我的Output.txt是空的时候。非常感谢。

2 个答案:

答案 0 :(得分:2)

使用awk单行:

awk '{print $1,$3,$(NF-1),$NF}' your.file

NF包含一行中的字段数。因此命令打印1.,3.倒数第二个和最后一个字段。

输出:

- Apple.java 980.9 ms
- ABC.java 45.9 ss
- TAS.java 75.9 ms
- GW.java 0.9 ss
- MYClass.java 7.9 ss
- Times.java 9.9 ss
- Sunday.java 85.9 ss
- Week.java 1000.9 ss

答案 1 :(得分:1)

GNU代码

sed -r 's/.*class\s(\S+)\s.*(\s[0-9.]+\s[ms][ms])$/- \1\2/' file

$ cat file
- class Apple.java executed by user ABC34 executable time 980.9 ms
- class ABC.java executed by user ABC34  executable time is in sec 45.9 ss
- class TAS.java executed by user ABC34  executable time 75.9 ms
- class GW.java executed by user ABC34  executable time is in sec 0.9 ss
- class MYClass.java executed by user ABC34  executable time  is in sec 7.9 ss
- class Times.java executed by user ABC34  executable time  is in sec 9.9 ss
- class Sunday.java executed by user ABC34  executable time  is in sec 85.9 ss
- class Week.java executed by user ABC34  executable time  is in sec 1000.9 ss

$ sed -r 's/.*class\s(\S+)\s.*(\s[0-9.]+\s[ms][ms])$/- \1\2/' file
- Apple.java 980.9 ms
- ABC.java 45.9 ss
- TAS.java 75.9 ms
- GW.java 0.9 ss
- MYClass.java 7.9 ss
- Times.java 9.9 ss
- Sunday.java 85.9 ss
- Week.java 1000.9 ss