Substring in Bourne shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substring in Bourne shell
# 1  
Old 09-09-2008
Substring in Bourne shell

Shell used: Bourne shell
Environment: UNIX in a Sun machine

I found myself struggling to figure out how to split a string. I kept wishing it's in Java that I can do it real quick.

Basically I have a string as follows:
"http://machine1:8080/aaa/bbb"

I need to get the value as follows to access the directory:
"/aaa/bbb"

I know that I need to eliminate "http://machine1:8080" and this value is read from a config file. So the length is not fixed and the value is not fixed.

I have been thinking of using grep or cut but I fail to get what I need.

Please advise. Thanks
# 2  
Old 09-09-2008
Try expr

expr index <string> <substring> returns the offset of a small string in a larger string
expr substr <string> <offset> <[length]> returns a substring
# 3  
Old 09-09-2008
Why can't you try sed?

Code:
echo "$string" | sed 's/\(.*\):[0-9]\{4\}\(.*\)/\2/'

Quote:
["/tf2/usr/tf01" ]>x="http://machine1:8080/aaa/bbb"
["/tf2/usr/tf01" ]>echo $x
http://machine1:8080/aaa/bbb
["/tf2/usr/tf01" ]>echo $x |sed 's/\(.*\):[0-9]\{4\}\(.*\)/\2/'
/aaa/bbb
# 4  
Old 09-09-2008
Thanks a lot. You guys are really good.

It would be great if someone can explain:
sed 's/\(.*\):[0-9]\{4\}\(.*\)/\2/'

Thanks again
# 5  
Old 09-09-2008
I just hit a problem with sed. Sorry, I don't quite understand the command sed.

x="http://10.222.11.112:866/a/bb/c/d"
echo $x
echo $x |sed 's/\(.*\):[0-9]\{4\}\(.*\)/\2/'

Output:
http://10.222.11.112:866/a/bb/c/d
http://10.222.11.112:866/a/bb/c/d

I expect:
http://10.222.11.112:866/a/bb/c/d
/a/bb/c/d
# 6  
Old 09-09-2008
Quote:
Originally Posted by totziens
I just hit a problem with sed. Sorry, I don't quite understand the command sed.

x="http://10.222.11.112:866/a/bb/c/d"
echo $x
echo $x |sed 's/\(.*\):[0-9]\{4\}\(.*\)/\2/'

Output:
http://10.222.11.112:866/a/bb/c/d
http://10.222.11.112:866/a/bb/c/d

I expect:
http://10.222.11.112:866/a/bb/c/d
/a/bb/c/d


I have modified it .Earlier it will be expecting the port to a 4 digit number. I have changed it now to have 1-4 digits.

Code:
echo $x |sed 's/\(.*\):[0-9]\{1,4\}\(.*\)/\2/'

sed 's/\(.*\):[0-9]\{1,4\}\(.*\)/\2/' - identifying pattern upto the ip address
sed 's/\(.*\):[0-9]\{1,4\}\(.*\)/\2/' - looking for a pattern of digits of size 1 to 4
sed 's/\(.*\):[0-9]\{1,4\}\(.*\)/\2/' - looking for the pattern after the port number(here, directory)
sed 's/\(.*\):[0-9]\{1,4\}\(.*\)/\2/' - display the second group of pattern.ie,

Last edited by dennis.jacob; 09-09-2008 at 09:16 AM..
# 7  
Old 09-09-2008
how about:

Code:
#  echo $x |sed 's/.*:[0-9]*//'
/a/bb/c/d

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Cybersecurity

'Shell Shock' vulnerability in Bourne shell

A severe vulnerability was discovered in Bourne shell. Just google for: bash vulnerability ... for more details. (5 Replies)
Discussion started by: Cochise
5 Replies

2. Shell Programming and Scripting

Bourne shell & Korn shell

Could some one tell me the difference btw Bourne shell and the Kshell? Which is more flexible and reliable in terms of portability and efficiency. When i type the following command .. $ echo $SHELL yields me /bin/sh Does this tells me that I am in Bourne shell. If yes, how can i get... (6 Replies)
Discussion started by: bobby1015
6 Replies

3. Shell Programming and Scripting

Bourne/C shell help

Exercise Five Write a Bourne shell script which: • Professionalism: plan for this from the start. • Has one command line argument. • If the command line argument is a directory then the script should output the number of files in the directory. • If the command line argument is an ordinary... (2 Replies)
Discussion started by: moesom
2 Replies

4. Shell Programming and Scripting

Is there any command in the Bourne shell?

Hi, The problem I have is that I want to create a list of folders whose names are read from a text file but the file names are in decimal. Each letter consists of an octet and the end of the folder name is defined by the white space character (0032) For example, we have in the text... (2 Replies)
Discussion started by: Gengis-Kahn
2 Replies

5. Shell Programming and Scripting

How to activate Korn Shell functionnalities in Bourne Shell

Hi All I have writing a Korn Shell script to execute it on many of our servers. But some servers don't have Korn Shell installed, they use Borne Shell. Some operations like calculation don't work : cat ${file1} | tail -$((${num1}-${num2})) > ${file2} Is it possible to activate Korn Shell... (3 Replies)
Discussion started by: madmat
3 Replies

6. Shell Programming and Scripting

I need to understand the differences between the bash shell and the Bourne shell

I do not claim to be an expert, but I have done things with scripts that whole teams of folks have said can not be done. Of course they should have said we do not have the intestinal fortitude to git-r-done. I have been using UNIX actually HPUX since 1992. Unfortunately my old computer died and... (7 Replies)
Discussion started by: awk_sed_hello
7 Replies

7. UNIX for Dummies Questions & Answers

Bourne-again shell

Hi guys !! well i'm still new in learning UNIX , and actually i'm still studying it by myself .. anyway, some people told me the Bourne-again shell is a good version of UNIX to work on , and i tried to download yesterday but i didn't know how to start it ...... the ReadMe file associated with... (3 Replies)
Discussion started by: mrsamer
3 Replies

8. UNIX for Dummies Questions & Answers

bourne shell programming help!

hey, i have 2 files... orders and products how do i do calculations on the order using the products ? say if the products file is: a123:shirt:10.00 zz123:nice shirt:19.95 and the order file is: 05/08/30 a123 10 zz123 3 Jun-3-1994 a123 2 2005.06.23 a123 2 (1 Reply)
Discussion started by: ganjakh0r
1 Replies

9. Shell Programming and Scripting

Bourne Shell and Arrays

Hi everyone, first post here so please be gentle :-) I normally likle to script in Bourne Shell simply for guarenteed compatibility across any system I might run across but this latest problem has me stumped. Arrays is a rather significant construct missing from sh and after finding a way to... (2 Replies)
Discussion started by: Unbeliever
2 Replies

10. Shell Programming and Scripting

bourne shell script

Hi all, Can somebody answer the following query Thanks, Srinivas A shell program that takes one or any number of file directory names as input; sorts the directories given as parameters jointly in the ascending or decending order of choice For EX : dips abc etc desc will sort the files... (2 Replies)
Discussion started by: psrinivas
2 Replies
Login or Register to Ask a Question