Cut the string

---------- Post updated at 10:31 AM ---------- Previous update was at 10:28 AM ----------
Hello,
I am trying to get the string cut based on the following needs:

String1=Submitted request 25574824 for CONCURRENT SQLAP RUAPACTUALSEXT Y RUAPACTUALS122313100616.dat "2013/01/12 14:50:44" "2013/01/16 16:20:21" Normal completion

string2=RUAPACTUALS122313100616.dat "2013/01/12 14:50:44" "2013/01/16 16:20:21"

I need to look for the LAST occurrence of string2 in string1 (in this example there is only one occurrence) and get the rest of the text in string1 by ignoring the first space.

In the above example, I am looking for output of
Normal completion

Any help is greatly appreciated.

>echo $string1
Submitted request 25574824 for CONCURRENT SQLAP RUAPACTUALSEXT Y RUAPACTUALS122313100616.dat "2013/01/12 14:50:44" "2013/01/16 16:20:21" Normal completion
>echo $string2
RUAPACTUALS122313100616.dat "2013/01/12 14:50:44" "2013/01/16 16:20:21"
>echo <some unix command to get the output>
Normal completion

Dear cartrider,

  • What have you tried so far?
  • What is your OS and version?
  • Have you searched for help before posting?
  • Have you looked at the manual page for cut ?

Most importantly, what have you tried so far?

Presuming that you are at the shell prompt or in a shell script and that you want to get the last bit of $string1, but how should we split it? You would need to tell us:-

  • What format the string will always be in (space delimited, fixed width, number of fields etc.
  • What other output apart from Normal completion we will have to expect

There are lots of ways to do this, but it would be best if you can steer us what you are considering and therefore you will learn better with what you are familiar with.

Robin

Format of string 1 is space delimited, but can have any number of fields.
'Normal completion' is just one output. It could have any number of fields in place of 'Normal completion'

I have tried the following cut command, but keep getting invalid delimiter. Even if it worked, it may give me only 'Normal' as the output.

cut1=`echo $string1|cut -d $string2 -f1`
cut1=`echo $string1|cut -d '$string2' -f1`

Assuming you're using a recent bash (allowing for "here strings"), which you do not mention despite rbatte1's questions, try this

awk -vS2="$string2" '$0 ~ S2 {sub ("^.*" S2 " ", ""); print}' <<< $String1 
Normal completion
DEV> echo $str1
Submitted request 25574824 for CONCURRENT SQLAP RUAPACTUALSEXT Y RUAPACTUALS122313100616.dat "2013/01/12 14:50:44" "2013/01/16 16:20:21" Normal completion
DEV> echo $str2
RUAPACTUALS122313100616.dat "2013/01/12 14:50:44" "2013/01/16 16:20:21"
DEV> awk -vS2="$str2" '$0 ~ S2 {sub ("^.*" S2 " ", ""); print}' 
<<< $Str1 
DEV>           

No output.

No surprise.

The "here string" needs to be on the same line. And, watch your variables' case(s).

An approach in bash:

#!/bin/bash

string1='Submitted request 25574824 for CONCURRENT SQLAP RUAPACTUALSEXT Y RUAPACTUALS122313100616.dat "2013/01/12 14:50:44" "2013/01/16 16:20:21" Normal completion'
string2='RUAPACTUALS122313100616.dat "2013/01/12 14:50:44" "2013/01/16 16:20:21"'

[[ "$string1" =~ "$string2" ]] && echo "${string1/*$string2 /}"