Extract string between two special chracters

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Extract string between two special chracters
# 1  
Old 01-27-2018
Extract string between two special chracters

Hi Folks -

I'm trying to extract the string between two special characters, the "-" and "." symbols.

The string format is as such:
Code:
_PBCS_URL_PRD=https://plan-a503777.pbcs.us6.ocloud.com
_PBCS_URL_TST=https://pln-test-a503777.pbcs.us6.ocloud.com

In the above case, I need to extract "a503777".

Notice _PBCS_URL_TST has two "-" symbols.

I've tried the following with no success:

Code:
echo "${_PBCS_URL_PRD}" | sed -e 's/-\(.*\)./\1/g'

# 2  
Old 01-27-2018
As always:
What operating system are you using?

What shell are you using?
# 3  
Old 01-27-2018
Does this code produce the result you want?
Code:
echo "${_PBCS_URL_PRD}" |  perl -nle '/-(\w+)\./ and print $1'

# 4  
Old 01-28-2018
Hi Don -

BASH.

Thanks!
# 5  
Old 01-28-2018
Try
Code:
echo "${_PBCS_URL_TST}" | sed -e 's/^.*-\|[.].*$//g'
a503777

# 6  
Old 01-28-2018
With bash or ksh or any other POSIX-conforming shell, you can use:
Code:
_PBCS_URL_PRD=https://plan-a503777.pbcs.us6.ocloud.com
_PBCS_URL_TST=https://pln-test-a503777.pbcs.us6.ocloud.com

STRING=${_PBCS_URL_PRD##*-}
echo ${STRING%%.*}

STRING=${_PBCS_URL_TST##*-}
echo ${STRING%%.*}

producing the output:
Code:
a503777
a503777

without needing to invoke any utilities that are not built into the shell itself. (Although there is no requirement that echo be built into the shell, almost all recent shells do provide it as a built-in.)
# 7  
Old 01-28-2018
Hi Don -

Thank you so much!

That worked like a charm and it's great to know its adaptable across a few different OS's.

Code:
_PBCS_URL=${_PBCS_URL_TST}; STRING=${_PBCS_URL##*-}; _DOMAIN=${STRING%%.*}

RudiC and Aia, both of those options worked great too!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extract string between two special characters

Hi, I have a file that looks something like >1-18*anc... (12 Replies)
Discussion started by: jyu429
12 Replies

2. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

3. Shell Programming and Scripting

Extract string between 2 special characters

Hi, I have a unix file with contents as below Line1: ABC MNN X$$QWERTY$$ JKL Line2: HELLO $$HOW$$ ARE $$YOU$$ DOING i want to extract the string between $$ and $$ ie i want the output as QWERTY HOW YOU i want those strings seperated by some character say | desired output is... (7 Replies)
Discussion started by: vinredmac
7 Replies

4. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. UNIX for Dummies Questions & Answers

tailing a file which contains Control chracters

Hi. I have a log file which gets updated by a java process and it uses ASCII STX and ETX characters (i.e CTRL-B and CTRL-C characters) to demarcate each XML message logged. so the format of the file is something like STX XML_MESSAGE1 .. .. ETX STX XML_MESSAGE2 .. .. ETX each XML... (4 Replies)
Discussion started by: gregoryp
4 Replies

6. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

7. Shell Programming and Scripting

Perl Script Syntax to Extract Everything After Special Character

Hi, I am writing a Perl script that reads in many lines, if a line meets the criteria I want to edit, it. For example, the script will return the following example line... test=abc123 All I want to do is strip off the "test=" and just be left with the abc123. In my script I can easily... (3 Replies)
Discussion started by: edrichard
3 Replies

8. Shell Programming and Scripting

Add string after another string with special characters

Hello everyone, I'm writing a script to add a string to an XML file, right after a specified string that only occurs once in the file. For testing purposes I created a file 'testfile' that looks like this: 1 2 3 4 5 6 6 7 8 9 And this is the script as far as I've managed: ... (2 Replies)
Discussion started by: heliode
2 Replies

9. Shell Programming and Scripting

escape chracters

hi, how to echo \\ in unix ie echo the path \\dir1\dir2\\dir3 thanks, Sam (7 Replies)
Discussion started by: sam99
7 Replies

10. Shell Programming and Scripting

Need help to extract a string delimited by any special character

I have a string as follows IS*blahblah TED~blahblah etc. I want to list down only IS and TED Can someone help me? (24 Replies)
Discussion started by: kumariak
24 Replies
Login or Register to Ask a Question