bash 获取某个单词后的子串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22708759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:02:15  来源:igfitidea点击:

Get a substring after a certain word

bashshellparsing

提问by Dennis

The string looks as follows:

该字符串如下所示:

Event Id: 971
Time    : Fri Mar 28 03:50:03 2014
Type    : INFO
User    : ehwe
Message :
        Oracle_Connector_1,0: Number of rows fetched on the current node: 0.

I need to get the sub string that follows the Message :. So I in this case I need this Oracle_Connector_1,0: Number of rows fetched on the current node: 0.

我需要获取Message :后面的子字符串。所以在这种情况下我需要这个 Oracle_Connector_1,0: Number of rows fetched on the current node: 0。

I've tried using b=${a%'Message :'*}format but that didn't help.

我试过使用 b=${a%'Message :'*}格式,但这没有帮助。

Little help, please?

有点帮助好吗?

Update:

更新:

Some log entries might look like the next one:

一些日志条目可能如下所示:

Event Id: 970
Time    : Fri Mar 28 03:50:03 2014
Type    : FATAL
User    : ehwe
Message :
    Oracle_Connector_1,0: [IIS-CONN-ORA-001003] The OCI function OCIStmtExecute returned status -1. Error code: 6,550, Error message: ORA-06550: line 2, column 14:
    PLS-00201: identifier 'IT' must be declared
    ORA-06550: line 2, column 14:
    PL/SQL: Item ignored
    ORA-06550: line 5, column 5:
    PLS-00320: the declaration of the type of this expression is incomplete or malformed
    ORA-06550: line 5, column 5:
    PL/SQL: Statement ignored
    ORA-06550: line 8, column 26:
    PLS-00320: the declaration of the type of this expression is incomplete or malformed
    ORA-06550: line 8, column 7:
    PL/SQL: Statement ignored. (CC_OraStatement::executePlSql, file CC_OraStatement.cpp, line 2,746)

采纳答案by fedorqui 'SO stop harming'

You can use this:

你可以使用这个:

awk 'f; /Message/ {f=1}' file
  • If the flag fis set, condition is true so awkprints the line.
  • Whenever the text Messageappears, the flag fis set to 1.
  • 如果f设置了标志,则条件为真,因此awk打印该行。
  • 每当文本Message出现时,标志f设置为 1。


Update

更新

If you want to print exactly the line after Messageoccurs, you can do:

如果要在Message发生后准确打印该行,可以执行以下操作:

$ awk 'f {print; exit} /Message/ {f=1}' file
    Oracle_Connector_1,0: [IIS-CONN-ORA-001003] The OCI function OCIStmtExecute returned status -1. Error code: 6,550, Error message: ORA-06550: line 2, column 14:

It is the same as above, just that after printing for the first time, it stops reading the file and exits.

和上面一样,只是第一次打印后停止读取文件和exits.

To use a variable as input instead of a file, use:

要使用变量而不是文件作为输入,请使用:

awk 'f {print; exit} /Message/ {f=1}' <<< "$var"

See an example:

看一个例子:

$ echo "$var"
hello
how are you

$ awk '1' <<< "$var"
hello
how are you

回答by Aslan

grep -A1 Message | tail -1should do the trick. -A1 tells grep the write one more line to the output (after 1) when a match is found.

grep -A1 Message | tail -1应该做的伎俩。-A1 告诉 grep 在找到匹配项时再向输出写入一行(在 1 之后)。

回答by Henk Langeveld

I expect ${a%'Message :'*}to return the firstfour lines. If $acontains the entire message above, reverse the match:

我希望${a%'Message :'*}返回四行。如果$a包含上面的整个消息,则反转匹配:

a="Event Id: 971
Time    : Fri Mar 28 03:50:03 2014
Type    : INFO
User    : ehwe
Message :
        Oracle_Connector_1,0: Number of rows fetched on the current node: 0."

echo "${a#*Message :[$IFS]}"

Outputs:

输出:

        Oracle_Connector_1,0: Number of rows fetched on the current node: 0.

Note: Using [$IFS]because I'm too lazy to figure out an explicit match with the newline character. That can be improved.

注意:使用[$IFS]是因为我懒得找出与换行符的显式匹配。这是可以改进的。

Tested in bashand ksh93

测试在bashksh93