Denys
Denys

Reputation: 4557

Get a substring after a certain word

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.

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

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)

Upvotes: 2

Views: 8800

Answers (3)

fedorqui
fedorqui

Reputation: 289555

You can use this:

awk 'f; /Message/ {f=1}' file
  • If the flag f is set, condition is true so awk prints the line.
  • Whenever the text Message appears, the flag f is set to 1.

Update

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

$ 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.

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

Upvotes: 2

Henk Langeveld
Henk Langeveld

Reputation: 8446

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

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.

Tested in bash and ksh93

Upvotes: 1

Aslan
Aslan

Reputation: 182

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

Upvotes: 1

Related Questions