我是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
Run Code Online (Sandbox Code Playgroud)
我需要在Node之后提取文本:并将其添加到Info之后的文本:要显示在一行上,输出要重定向到新文件.我正在尝试awk和sed,但还没想到它.非常感谢.
示例输出如下所示:
xyz Trusted certif ok
abc Trusted certif ok
dbf Trusted certif not ok
Run Code Online (Sandbox Code Playgroud)
试着这样做:
在awk
awk -F: '/^Node/{v=$2}/^INFO/{print v $2}' file.txt
Run Code Online (Sandbox Code Playgroud)
在bash中:
while IFS=: read -r c1 c2; do
[[ $c1 == Node ]] && var=$c1
[[ $c1 == INFO ]] && echo "$var$c2"
done < file.txt
Run Code Online (Sandbox Code Playgroud)
in perl:
perl -F: -lane '
$v = $F[1] if $F[0] eq "Node";
print $v, $F[1] if $F[0] eq "INFO"
' file.txt
Run Code Online (Sandbox Code Playgroud)
在python中(在文件中,用法:) ./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
Run Code Online (Sandbox Code Playgroud)