如何使用grep输出为文本添加前缀

时间:2015-09-21 19:10:31

标签: windows grep

我想从ABCD_ERR.LOG读取7:00:59到08:00:59之间记录的日志,并搜索这些日志行中的一些字符串,并将搜索到的输出行存储到output.txt。但我想在输出行之前添加inputfilename ie.ABCD_ERR.LOG。 例如, ABCD_ERR.LOG包含

之类的日志
        Server 07972 006856 06:06:14.203 CleanUp - Elapsed 16 usec - Info : Session does not contain modified objects in cache.
Communications 06480 006584 07:01:56.140 Elapsed 94592 usec - Container Temperature (80)
        Server 07972 006860 07:06:20.281 Elapsed 516245941539 usec - No of resources in array = 0
Communications 06480 006584 07:11:56.140 Elapsed 92355 usec - Container Temperature (80)
        Server 07972 006860 07:16:21.296 Elapsed 516846919543 usec - No of resources in array = 0
Communications 06480 006584 07:21:56.140 Elapsed 89978 usec - Container Temperature (80)
        Server 07972 006860 07:26:22.296 CleanUpThread - Elapsed 517447899556 usec  
            BE 05452 009252 08:37:25.375 Elapsed 87 usec - Updating SESSION_ID
        Server 04616 004744 09:24:12.437 Initialize() - Elapsed 7132 usec

Regfile.txt包含

Container Temperature
Updating SESSION_ID

以下是我正在做的事情:

grep -E "(07:[0-9][0-9]:|08:[0-9][0-9]:).*" ABCD_ERR.LOG|grep -E -f Regfile.txt > output.txt

但是我希望输出(带有输出行的文件名前缀)为

ABCD_ERR.LOG:Communications 06480 006584 07:01:56.140 Elapsed 94592 usec - Container Temperature (80)
ABCD_ERR.LOG:Communications 06480 006584 07:11:56.140 Elapsed 92355 usec - Container Temperature (80)
ABCD_ERR.LOG:Communications 06480 006584 07:21:56.140 Elapsed 89978 usec - Container Temperature (80)
ABCD_ERR.LOG:       BE 05452 009252 08:37:25.375 Elapsed 87 usec - Updating SESSION_ID

有没有办法获得如上所示的所需输出? 我已经尝试过/ dev / null选项,但是在标准输出上给出错误" grep:write error:管道正在关闭。"因为输入到第二个grep不是文件。 我在windows 7中使用grep

2 个答案:

答案 0 :(得分:0)

这是awk的工作,而不是grep。这将按照你说你遇到问题的方式进行(预先设置文件名):

$ awk 'NR==FNR{re=(re?re"|":"")$0; next} /07:[0-9][0-9]:|08:[0-9][0-9]:/ && ($0 ~ re){print FILENAME":"$0}' Regfile.txt ABCD_ERR.LOG
ABCD_ERR.LOG:Communications 06480 006584 07:01:56.140 Elapsed 94592 usec - Container Temperature (80)
ABCD_ERR.LOG:Communications 06480 006584 07:11:56.140 Elapsed 92355 usec - Container Temperature (80)
ABCD_ERR.LOG:Communications 06480 006584 07:21:56.140 Elapsed 89978 usec - Container Temperature (80)
ABCD_ERR.LOG:            BE 05452 009252 08:37:25.375 Elapsed 87 usec - Updating SESSION_ID

但请注意,用于标识目标范围内时间的正则表达式是错误的。这是您可以真正检查同一天给定时间内给定时间的一种方法:

$ cat tst.awk
(t2s($4) >= t2s("7:00:59")) && (t2s($4) <= t2s("8:00:59"))

function t2s(time,      a) {
    split(time,a,/:/)
    return (((a[1] * 60) + a[2]) * 60) + a[3]
}
$
$ awk -f tst.awk file
Communications 06480 006584 07:01:56.140 Elapsed 94592 usec - Container Temperature (80)
        Server 07972 006860 07:06:20.281 Elapsed 516245941539 usec - No of resources in array = 0
Communications 06480 006584 07:11:56.140 Elapsed 92355 usec - Container Temperature (80)
        Server 07972 006860 07:16:21.296 Elapsed 516846919543 usec - No of resources in array = 0
Communications 06480 006584 07:21:56.140 Elapsed 89978 usec - Container Temperature (80)
        Server 07972 006860 07:26:22.296 CleanUpThread - Elapsed 517447899556 usec

那么你可以将两个脚本结合起来做我认为你真正想要的事情:

$ cat tst.awk
NR==FNR { re=(re?re"|":"")$0; next }

($0 ~ re) && (t2s($4) >= t2s("7:00:59")) && (t2s($4) <= t2s("8:00:59")) { print FILENAME ":" $0 }

function t2s(time,      a) {
    split(time,a,/:/)
    return (((a[1] * 60) + a[2]) * 60) + a[3]
}

$ awk -f tst.awk Regfile.txt ABCD_ERR.LOG
ABCD_ERR.LOG:Communications 06480 006584 07:01:56.140 Elapsed 94592 usec - Container Temperature (80)
ABCD_ERR.LOG:Communications 06480 006584 07:11:56.140 Elapsed 92355 usec - Container Temperature (80)
ABCD_ERR.LOG:Communications 06480 006584 07:21:56.140 Elapsed 89978 usec - Container Temperature (80)

答案 1 :(得分:0)

最后我得到了我的问题的解决方案 这是我用过的:

grep -E "(07:[0-5][0-9]:|08:[0-5][0-9]:).*" ABCD_ERR.LOG | grep --label=ABCD_ERR.LOG -H -E -f Regfile.txt > output.txt