简体   繁体   English

Linux命令提取文本

[英]Linux command to extract a text

Text: 文本:

All rights reserved. ERROR: ORA-00257: archiver error. Connect internal only, error. SP2-0751: Unable to connect to oracle. Exiting SQL*Plus

My intended output: 我的预期输出:

ERROR: ORA-00257: archiver error.

My command: 我的命令:

sed 's/.*\(ERROR:.*\.\).*/\1/'

Actual Output: 实际输出:

ERROR: ORA-00257: archiver error. Connect internal only, error. SP2-0751: Unable to connect to oracle.

How can i tweak my sed command to display only this: 我如何调整sed命令以仅显示以下内容:

ERROR: ORA-00257: archiver error.

不要取所有字符( .* )后接点,而应取“除点之外的所有字符”( [^\\.]* )后接点:

sed 's/.*\(ERROR:[^\.]*\.\).*/\1/'

Sed use greedy match by default and it seems that sed doesn't support non-greedy match, so it will match as long as possible. sed默认情况下使用贪婪匹配,并且sed似乎不支持非贪婪匹配,因此它将尽可能长地匹配。

You can change .*\\. 您可以更改.*\\. in the parenthesis into [^.]*\\. 在括号中插入[^.]*\\. to match a non-dot character then follow a dot, this should behave like non-greedy match. 要匹配非点字符,然后跟随点,则其行为应类似于非贪婪匹配。 Or you can use a more powerfull regex engine like perl: 或者,您可以使用功能更强大的正则表达式引擎,例如perl:

perl -pe 's/.*(ERROR:.*?\\.).*/\\1/'

You can achieve the same with sed 您可以使用sed达到相同的目的

$ sed -r 's/.*(ERROR[^.]*\.).*/\1/' log

ERROR: ORA-00257: archiver error.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM