Substr/Instr in shell script or extract part of text

Hi,

I need to extract part of a text to two variables

text is

"PL/SQL procedure successfully completed. ERROR ----------------------------------------------------------------- Test Error Message PLUSVAR ---------- 1"

I want " Test Error Message " in one variable and " 1 " in another variable. I'm writing a shell script and i'm very new to UNIX.

Please help

Welcome to the forum.

Please show what you've tried and where you're stuck. How can the to-be-extracted substrings be identified?

SHELL_VAR="PL/SQL procedure successfully completed. ERROR ----------------------------------------------------------------- Test Error Message PLUSVAR ---------- 1
"
var=$(echo ${SHELL_VAR} | sed 's/.*--//')
echo $var
output
-----
1

This is extracting "1" but i want "Test Error Message" in a different variable just like what i did

There are several options. With just shell's "parameter expansion", try

TMP=${SHELL_VAR#*- } 
echo ${TMP%% -*}
Test Error Message PLUSVAR
echo ${SHELL_VAR##*-} 
1

Or, try an " awk quick liner":

echo ${SHELL_VAR} | awk -F"-*" '{print $(NF-1), $NF}'
 Test Error Message PLUSVAR   1

Also note that using PL/SQL exception handling mechanism will let you use built-in functions SQLCODE and SQLERRM to find out which error occurred and to get the associated error message.

Thank you for the replies. But i need to extract Test Error Message from:

PL/SQL procedure successfully completed. ERROR ----------------------------------------------------------------- Test Error Message PLUSVAR ---------- 1

Please help

In post #4 in this thread, RudiC showed you two ways to extract the two fields you said you wanted.

From your post #2 we might assume that you understand how to assign values to variables and how to print values stored in variables.

With what RudiC suggested, what else do you need to complete your task?

Please post the exact answer as i'm really new to shell scripting and unable to get the answer.

My requirement is to extract 'Test Error Message' from the below text and assign it to a variable.

'PL/SQL procedure successfully completed. ERROR ----------------------------------------------------------------- Test Error Message PLUSVAR ---------- 1'

I know we can use Cut or awk commands but i'm unable to get the answer

Please post the EXACT question. As already pointed out, extracting in several ways has been shown to you, and, according to your post#3, you know how to assign a command output - using "command substitution" - to a variable. Did you consider Yoda's proposal?

So - where are you stuck? And, SERIOUSLY!, start using code tags!

I'm stuck here. I could extract the text till:

 Test Error Message PLUSVAR   1

with the below command

echo ${SHELL_VAR} | awk -F"-*" '{print $(NF-1), $NF}'

But i want the Exact text Test Error Message

Try:

printf '%s\n' "$SHELL_VAR" | awk -F' -* ' '{printf("last field: \"%s\"\nnext to last field: \"%s\"\n", $NF, $(NF - 1))}'

which produces:

last field: "1"
next to last field: "Test Error Message PLUSVAR"

and adjust the format string in the awk printf function call to format the output of your two fields in whatever way you want to see them.

Note that the field separator here is a <space> followed by any number of <hyphen>s followed by another <space> to get rid of leading and trailing spaces in the fields you are extracting from the input line.

Try

awk -F" -* " -vINP="$V" 'BEGIN {$0 = INP; sub (/ [^ ]*$/, _, $(NF-1)); print $(NF-1); print $NF}'
Test Error Message
1