Cut the string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut the string
# 1  
Old 12-23-2013
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.


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

# 2  
Old 12-23-2013
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
# 3  
Old 12-23-2013
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.


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

# 4  
Old 12-23-2013
Assuming you're using a recent bash (allowing for "here strings"), which you do not mention despite rbatte1's questions, try this
Code:
awk -vS2="$string2" '$0 ~ S2 {sub ("^.*" S2 " ", ""); print}' <<< $String1 
Normal completion

# 5  
Old 12-23-2013
Code:
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.
# 6  
Old 12-23-2013
No surprise.

The "here string" needs to be on the same line. And, watch your variables' case(s).
# 7  
Old 12-23-2013
An approach in bash:
Code:
#!/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 /}"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut the data with a string that has \ in it

Hi Team, Here's the record in a file. abc\USER DEFINED\123\345\adf\aq1 Delimiter here is "\USER DEFINED\" Expected output: abc|123\345\adf\aq1 Find "\USER DEFINED\" and replace it with "|". Can anyone please help us to fix this issue? Please use CODE tags as required by... (5 Replies)
Discussion started by: kmanivan82
5 Replies

2. Shell Programming and Scripting

Cut string substr

friends as I can cut the first three characters in a string example: 400_FACTURACION_CANJES_20151217.txt Result 400 (2 Replies)
Discussion started by: tricampeon81
2 Replies

3. UNIX for Dummies Questions & Answers

Cut only time from string

Hi Guys, I have following different lines separated by multiple spaces. And i need to cut only the time part (16:53) from the string. How can we achieve it? remedy 29210 1 1 07:55 ? 00:06:20 /opt/bmc/java/jre1.6.0_17/bin/java -Xms1024m -Xmx2048m -XX:MaxPermSize=1024m... (12 Replies)
Discussion started by: Khushbu
12 Replies

4. Shell Programming and Scripting

Cut the string

Hi in a directory i've files having the following name for_category_info_19990101984301 for_catgry_meta_19991111214601 ------- I just want the name till year and month i.e; for_category_info_199901 for_catgry_meta_199911 How can i achieve the above string Thanks (2 Replies)
Discussion started by: smile689
2 Replies

5. Shell Programming and Scripting

How to cut a string from a variable

hello, i need to cut a string in unix but not from file ,, from variable, i searched on special line , and i need a specific data from this line , i get it on variable, how can i cut data that i need ??? merci (2 Replies)
Discussion started by: Reham.Donia
2 Replies

6. UNIX for Dummies Questions & Answers

how to cut prefix from a string

I have a file: chromosome1:436728 chromosome2:32892 ..... chromosome22:23781 I just want to get the number, not the prefix "chromosomeX", so I want to remove all the prefix ahead of the numbers. How can I do that?? Thanks!!! (PS: give me some very simple command so that I can understand... (4 Replies)
Discussion started by: kaixinsjtu
4 Replies

7. Shell Programming and Scripting

cut First charecter in any string

I wannt to cut first char of any string. str=A2465443 out put will be. str1=A str2=2465443. I tried str2=${?%srt} str1=${str#$str2} it is not working. (6 Replies)
Discussion started by: rinku
6 Replies

8. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

9. Shell Programming and Scripting

How to cut prefix from a string?

Hi folks, I have the following parameter setting: export ADMIN_HOST_NAME=http://hostname.com I want to define a new parameter,ADMIN_HOST_NAME_NEW,which based on $ADMIN_HOST_NAME but I need to remove the prefix "http://". The requested result for $ADMIN_HOST_NAME_NEW is hostname.com How... (2 Replies)
Discussion started by: nir_s
2 Replies

10. UNIX for Dummies Questions & Answers

how to cut a string from a variable

Hello All, I have a string type variable called "FULLSTR". This variable has a value of "path=/type/source/logs". I need to cut the characters after the character "=" to end of the string. Example: the new variable "MATCHSTR" should have value like this... "/type/source/logs" So... (2 Replies)
Discussion started by: kjaisan
2 Replies
Login or Register to Ask a Question