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
# 8  
Old 10-11-2017
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:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 10-11-2017 at 03:09 PM.. Reason: Added CODE tags.
# 9  
Old 10-11-2017
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!
# 10  
Old 10-12-2017
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; 10-13-2017 at 06:21 PM.. Reason: Add CODE tags again.
# 11  
Old 10-12-2017
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.
# 12  
Old 10-13-2017
Try
Code:
awk -F" -* " -vINP="$V" 'BEGIN {$0 = INP; sub (/ [^ ]*$/, _, $(NF-1)); print $(NF-1); print $NF}'
Test Error Message
1

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