Substr/Instr in shell script or extract part of text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substr/Instr in shell script or extract part of text
# 1  
Old 10-10-2017
Substr/Instr in shell script or extract part of text

Hi,

I need to extract part of a text to two variables

text is
Code:
"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

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 10-10-2017 at 11:51 AM.. Reason: Added CODE tags.
# 2  
Old 10-10-2017
Welcome to the forum.

Please show what you've tried and where you're stuck. How can the to-be-extracted substrings be identified?
# 3  
Old 10-10-2017
Code:
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

Last edited by Scott; 10-10-2017 at 12:00 PM.. Reason: Use code tags, please...
# 4  
Old 10-10-2017
There are several options. With just shell's "parameter expansion", try
Code:
TMP=${SHELL_VAR#*- } 
echo ${TMP%% -*}
Test Error Message PLUSVAR
echo ${SHELL_VAR##*-} 
1

Or, try an "awk quick liner":
Code:
echo ${SHELL_VAR} | awk -F"-*" '{print $(NF-1), $NF}'
 Test Error Message PLUSVAR   1

# 5  
Old 10-10-2017
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.
# 6  
Old 10-10-2017
Thank you for the replies. But i need to extract Test Error Message from:
Code:
PL/SQL procedure successfully completed. ERROR ----------------------------------------------------------------- Test Error Message PLUSVAR ---------- 1

Please help
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments as required by forum rules.

Last edited by Don Cragun; 10-11-2017 at 03:24 AM.. Reason: Add CODE and ICODE tags.
# 7  
Old 10-11-2017
Quote:
Originally Posted by vedavrath
Thank you for the replies. But i need to extract Test Error Message from:
Code:
PL/SQL procedure successfully completed. ERROR ----------------------------------------------------------------- Test Error Message PLUSVAR ---------- 1

Please help
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments as required by forum rules.
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract part of $USER in script

I need to be able to say "My name is $USER' when I use echo it work, when I use printf it print more 'my last name@.........'. How can I get just the part before the @ sign. I have to use printf. using UNIX. ------ Post updated at 01:17 PM ------ I used the activation code but I still... (2 Replies)
Discussion started by: sheltie042
2 Replies

2. Shell Programming and Scripting

regular expression with shell script to extract data out of a text file

hi i am trying to extract some specific data out of a text file using regular expressions with shell script that is using a multiline grep .. and the tool i am using is pcregrep so that i can get compatibility with perl's regular expressions for a sample data like this, i am trying to grab... (6 Replies)
Discussion started by: vemkiran
6 Replies

3. Shell Programming and Scripting

extract part of hostname in a script

I need to automate something as part of post processing in a script . Each project is identified by a 3 letter string which is part of hostname and based on hostname I need to copy a particular file to that machine from my distribution . here are hostnames pprdifeap01.corp.host.net (where... (1 Reply)
Discussion started by: gubbu
1 Replies

4. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

5. Shell Programming and Scripting

extract part of text file

I need to extract the following lines from this text and put it in different files. From xxxx@gmail.com Thu Jun 10 21:15:46 2010 Return-Path: <xxxxx@gmail.com> X-Original-To: xxx@localhost Delivered-To:xxxx@localhost Received: from ubuntu (localhost ) by ubuntu (Postfix) with ESMTP... (11 Replies)
Discussion started by: waxo
11 Replies

6. Shell Programming and Scripting

substr from a string in Shell script

Shell Scripting Gurus, I am having a hard time :confused: trying to figure out what I am doing wrong in my script. Below is the script snippet. It gives an error when it tries to execute the expression. a=`expr substr $stringZ 5 10` #!/bin/bash echo "Hello" stringZ="abcABC123ABCabc"... (3 Replies)
Discussion started by: yajaykumar
3 Replies

7. Shell Programming and Scripting

How to use substr to return data into a shell script variable?

I'm writing a shell script in which I need to be able to pull a portion of the file name out. I'm testing with the following code: x="O1164885.DAT" y=`ls -ltr *${x}|awk '{print substr($0,3)}'` echo ${x}|awk '{print substr($0,3)}' echo "y="$y I can echo it to the screen just fine but I... (3 Replies)
Discussion started by: ttunell
3 Replies

8. Shell Programming and Scripting

Substr in shell script

hi, i am using the following script to get the last digit of YEAR OY=`expr substr $YEAR 2 1` it is showing syntax Is this wrong or i need to change anything I am running this in sun solaris unix (7 Replies)
Discussion started by: gjithin
7 Replies

9. Shell Programming and Scripting

substr() thru awk Korn Shell Script

Hi, I am new stuff to learn substr() function through awk for writing the Korn shell script. Is there a way to copy from XXXX1234.ABCDEF to XXX1234 file names without changing both data files? I appreciate your time to response this email. Thanks, Steve (4 Replies)
Discussion started by: sbryant
4 Replies

10. Shell Programming and Scripting

Trouble using substr function with Bourne shell script

Hi, I'm a newbie to UNIX scripting and I'm having some trouble compiling my script. I'm using the Bourne Shell and cannot seem to use the substr function correctly. I'm trying to extract the last two digits of a year that's stored in a variable based off of a condition. I've searched the... (4 Replies)
Discussion started by: E2004
4 Replies
Login or Register to Ask a Question