Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing a String

Status
Not open for further replies.

jtanner

Technical User
Feb 18, 2007
39
US
Hello,

In a Bourne shell script I have a variable length string that is enclosed in parenthesis.

Examples
Code:
echo "$CONN1" (2msec)
echo "$CONN2" (10msec)
echo "$CONN3" (100msec)

How can I get ride of the parenthesis?

Thanks,

JT
 
You can use tr :
Code:
echo "$CONN1" | tr -d '()'



Jean-Pierre.
 
I'm assuming that (2msec) is what is contained in variable CONN1. Based on that assumption, this will remove the parens:

Code:
CONN1=`echo $CONN1| tr -d '()'`
echo $CONN1
2msec

Add a little color to your PUTTY terminal: faq52-6627
 
It appears that aigles is faster on the keyboard than me. [smile]

Add a little color to your PUTTY terminal: faq52-6627
 
The good old legacy Bourne shell...
what about this ?
Code:
expr "$CONN1" : '(\(.*\))'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
With sed:

Code:
CONN1=$(echo "${CONN1}"|sed 's/[()]//g')

or

Code:
CONN1=`echo "${CONN1}"|sed 's/[()]//g'`


HTH,

p5wizard
 
Guys,

All your suggestions were simple and brilliant.

Thanks,

JT
 
Here's a less "simple" solution ;-):

Code:
CONN1=$(echo "${CONN1}"|fold -w1|grep -v '('|grep -v ')'|tr -d '\n')


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top