简体   繁体   中英

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:

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.

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 -pe 's/.*(ERROR:.*?\\.).*/\\1/'

You can achieve the same with sed

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

ERROR: ORA-00257: archiver error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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