3

我是 Linux 新手,有一个非常大的文本日志文件可以从中提取。我想使用bash?

例如,该文件包含:

Node:xyz
Time:01/07/13 14:26:17
INFO: Trusted certif ok

Node:abc
Time:01/07/13 14:26:18
INFO: Trusted certif ok

Node:def
Time:01/07/13 14:26:18
INFO: Trusted certif not ok

我需要在 Node: 之后提取文本并将其添加到 Info: 之后的文本中以显示在一行上,输出重定向到一个新文件。我正在尝试 awk 和 sed,但还没有弄清楚。非常感谢帮助。

示例输出如下所示:

xyz Trusted certif ok
abc Trusted certif ok
dbf Trusted certif not ok
4

4 回答 4

12

尝试这样做:

awk -F: '/^Node/{v=$2}/^INFO/{print v $2}' file.txt

中:

while IFS=: read -r c1 c2; do
    [[ $c1 == Node ]] && var=$c1
    [[ $c1 == INFO ]] && echo "$var$c2"
done < file.txt

中:

perl -F: -lane '
    $v = $F[1] if $F[0] eq "Node";
    print $v, $F[1] if $F[0] eq "INFO"
' file.txt

中(在文件中,用法 : ./script.py file.txt):

import sys
file = open(sys.argv[1])
while 1:
    line = file.readline()
    tpl = line.split(":")
    if tpl[0] == "Node":
        var = tpl[0]
    if tpl[0] == "INFO":
        print var, tpl[1]
    if not line:
        break
于 2013-01-07T22:14:10.413 回答
2

使用 sed:

sed -n '/^Node/N;/Time/N;s/^Node:\([^\n]*\)\n[^\n]*\n[^ ]* /\1 /p' input
于 2013-01-07T22:17:35.810 回答
0
sed -n 'N;N;s/\n.*\n/ /;s/\S*://g;p;n' file
于 2013-05-28T08:46:43.753 回答
0
perl -F: -lane '$x=$F[1] if(/^Node:/);if(/^INFO:/){print "$x".$F[1];}' your_file

测试如下:

> cat temp
Node:xyz
Time:01/07/13 14:26:17
INFO: Trusted certif ok

Node:abc
Time:01/07/13 14:26:18
INFO: Trusted certif ok

Node:def
Time:01/07/13 14:26:18
INFO: Trusted certif not ok

> perl -F: -lane '$x=$F[1] if(/^Node:/);if(/^INFO:/){print "$x".$F[1];}' temp
xyz  Trusted certif ok
abc  Trusted certif ok
def  Trusted certif not ok
于 2013-01-08T11:06:22.993 回答