Extract string between any two Alphanumaric strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract string between any two Alphanumaric strings
# 1  
Old 03-03-2011
Extract string between any two Alphanumaric strings

Note: Subject line correction --> any two Alphanumaric strings = any two strings

I am trying to come-up with geneic code, that can give a string between any two given strings in a given line(first occurance). Here input text line can have control characters.

Below code works as long as search strings do not have control characters. I know why this is failing,, but I need code that can handle special characters and reusable, Even if it means writing a function with multiple lines of code. But Love to see if that can be done in a single line.!!

How can we do that in ksh/bash/perl?

Code:
 
test.sh
# get a string between two AlphaNumeric character strings. 
v1='aa'; v2='cc'
echo "aaaabbbbbccccc" | sed -e "s/.*${v1}//;s/${v2}.*//"
 
# This works as long as two search strings do not have special chacters.
$> test.sh   
bbbbb

Code:
 
# This fails due to control characters in sed command. 
test.sh 
# Get string between any two search strings from input line.
v1='$?'; v2='cc'
echo "aa$?bbbbbccccc" | sed -e "s/.*${v1}//;s/${v2}.*//"
 
$> test.sh
aa0bbbbb


Last edited by kchinnam; 03-03-2011 at 12:17 PM.. Reason: color coded to make it more clear
# 2  
Old 03-03-2011
Did you try this?
Code:
v1='\$\?'

# 3  
Old 03-03-2011
I am not trying to do this for a known fixed search string,,
I need code that can handle search strings ${v1}, ${v2} with special characters. I can do this using advanced languages like Java.
Does sed or awk have any magic that can handle it in a one line command!.
# 4  
Old 03-03-2011
is it not the initial evaluation on your echo causing the issue:

Code:
#   echo "aa$?bbbbbccccc"
aa0bbbbbccccc

#   echo "aa\$?bbbbbccccc"
aa$?bbbbbccccc

or
#   echo 'aa$?bbbbbccccc'
aa$?bbbbbccccc

allows:
#   echo 'aa$?bbbbbccccc'  | sed -e "s/.*${v1}//;s/${v2}.*//"
bbbbb

# 5  
Old 03-03-2011
Thanks Tytalus, as long as I have my search strings and input string in single quotes, it works..

Following input string has two matching occurances 1. bbbb 2. dddd. Why sed prints last occurance of matched string?

How can I control which matched string gets printed. Say I want to print only first occurance or both matched strings seperated by something(comma!)?

Code:
$> v1='$?'; v2='cc'; echo 'aa$?bbbbccccc$?ddddccc' | sed -e "s/.*${v1}//;s/${v2}.*//"
 
dddd


Last edited by kchinnam; 03-03-2011 at 01:15 PM.. Reason: enhanced text
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Issue when using egrep to extract strings (too many strings)

Dear all, I have a data like below (n of rows=400,000) and I want to extract the rows with certain strings. I use code below. It works if there is not too many strings for example n of strings <5000. while I have 90,000 strings to extract. If I use the egrep code below, I will get error: ... (3 Replies)
Discussion started by: forevertl
3 Replies

2. UNIX for Dummies Questions & Answers

Extract strings based on the value

I have a file with multiple columns (in this case, the file has 3 columns): NM_001006304 (-33.7) XM_418228 (-38.4) JN880447 (-33.7) CR387600 (-33.7) CR524203 (-36.3) GALGA_6AKII_KRT75 (-33.7) GALGA25_SC7 (-31.9) CR352795 (-36.3) NM_204172 (-31.7) NM_204137 (-31.9) NM_001030561 (-36.3) AB011672... (7 Replies)
Discussion started by: yuejian
7 Replies

3. UNIX for Dummies Questions & Answers

Extract code between 2 strings.

Hi, Im having some problems with this. I have loaded a file with html code. All code is placed in the same line. I want to get everything between two given strings (including these strings and get only the first appearance). Example: File contains <html><body><a href='a.html'>abc</a><a... (5 Replies)
Discussion started by: ngb
5 Replies

4. Shell Programming and Scripting

sed to extract all strings

Hi, I have a text file containing 2 lines as follows: I'm trying to extract all the strings following an "AME." The output would be as follows: BUSINESS_UNIT PROJECT_ID ACTIVITY_ID RES_USER1 RESOURCE_ID_FROM ANALYSIS_TYPE BI_DISTRIB_STATUS BUSINESS_UNIT PROJECT_ID ACTIVITY_ID... (5 Replies)
Discussion started by: simpletech369
5 Replies

5. Shell Programming and Scripting

Extract two strings from a file and create a new file with these strings

I have the following lines in a log file. It would be great if some one can help me to create a new file with the just entries in the below format. 66.150.161.195 HPSAC=Z05 66.150.161.196 HPSAC=A05 That is just extract the IP address and the string DPSAC=its value 66.150.161.195 -... (1 Reply)
Discussion started by: Tuxidow
1 Replies

6. Shell Programming and Scripting

Search for string in a file, extract two another strings and concatenate to a variable

I have a file with <suit:run date="Trump Tue 06/19/2012 11:41 AM EDT" machine="garg-ln" build="19921" level="beta" release="6.1.5" os="Linux"> Need to find word "build" then extract build number, which is 19921 also release number, which is 6.1.5 then concatenate them to one variable as... (6 Replies)
Discussion started by: garg
6 Replies

7. Shell Programming and Scripting

Extract strings from file - Help

Hi, I have a file say with following lines (the lines could start from any column and there can be many many create statements in the file) create table table1....table definition... insert into table1 values..... create or replace view view1....view definition.... What i want is to... (2 Replies)
Discussion started by: whoami191
2 Replies

8. Shell Programming and Scripting

How to Extract text between two strings?

Hi, I want to extract some text between two strings in a line i am using following command i.e; awk '/-string1/,/-string2/' filename contents of file is--- line1 line2 aaa -bbb -ccc -string1 c,d,e -string2 line4 but it is showing complete line which is having searched strings. aaa... (19 Replies)
Discussion started by: emresearch
19 Replies

9. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

10. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies
Login or Register to Ask a Question