Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Substr/Instr in shell script or extract part of text

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 1 Week Ago
vedavrath vedavrath is offline
Registered User
 
Join Date: Oct 2017
Last Activity: 12 October 2017, 10:18 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
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.

Code:
'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

Moderator's Comments:
Substr/Instr in shell script or extract part of text Please use CODE tags as required by forum rules!

Last edited by RudiC; 1 Week Ago at 02:09 PM.. Reason: Added CODE tags.
Sponsored Links
    #9  
Old Unix and Linux 1 Week Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 October 2017, 5:17 PM EDT
Location: Aachen, Germany
Posts: 11,483
Thanks: 310
Thanked 3,561 Times in 3,276 Posts
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!
Sponsored Links
    #10  
Old Unix and Linux 1 Week Ago
vedavrath vedavrath is offline
Registered User
 
Join Date: Oct 2017
Last Activity: 12 October 2017, 10:18 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
I'm stuck here. I could extract the text till:

Code:
 Test Error Message PLUSVAR   1

with the below command

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

But i want the Exact text Test Error Message

Last edited by MadeInGermany; 1 Week Ago at 05:21 PM.. Reason: Add CODE tags again.
    #11  
Old Unix and Linux 1 Week Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 22 October 2017, 2:06 AM EDT
Location: San Jose, CA, USA
Posts: 10,554
Thanks: 554
Thanked 3,697 Times in 3,154 Posts
Try:

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

which produces:

Code:
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.
Sponsored Links
    #12  
Old Unix and Linux 1 Week Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 October 2017, 5:17 PM EDT
Location: Aachen, Germany
Posts: 11,483
Thanks: 310
Thanked 3,561 Times in 3,276 Posts
Try

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

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
regular expression with shell script to extract data out of a text file vemkiran Shell Programming and Scripting 6 06-26-2012 07:41 AM
extract part of text file waxo Shell Programming and Scripting 11 06-22-2010 06:18 PM
substr from a string in Shell script yajaykumar Shell Programming and Scripting 3 08-06-2008 12:51 PM
Substr in shell script gjithin Shell Programming and Scripting 7 05-10-2008 04:40 PM
substr() thru awk Korn Shell Script sbryant Shell Programming and Scripting 4 01-24-2008 10:16 AM



All times are GMT -4. The time now is 10:28 PM.