我是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
答案 0 :(得分:8)
尝试这样做:
awk 中的
awk -F: '/^Node/{v=$2}/^INFO/{print v $2}' file.txt
bash中的:
while IFS=: read -r c1 c2; do
[[ $c1 == Node ]] && var=$c1
[[ $c1 == INFO ]] && echo "$var$c2"
done < file.txt
perl中的:
perl -F: -lane '
$v = $F[1] if $F[0] eq "Node";
print $v, $F[1] if $F[0] eq "INFO"
' file.txt
<{3>}中的(在文件中,用法:./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
答案 1 :(得分:0)
使用sed:
sed -n '/^Node/N;/Time/N;s/^Node:\([^\n]*\)\n[^\n]*\n[^ ]* /\1 /p' input
答案 2 :(得分: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
答案 3 :(得分:0)
sed -n 'N;N;s/\n.*\n/ /;s/\S*://g;p;n' file