The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to parse a string efficiently sandiego_coder Shell Programming and Scripting 4 05-13-2008 09:12 AM
Parse String Using Sed racbern Shell Programming and Scripting 4 04-23-2008 09:14 AM
how to parse this string hcliff Shell Programming and Scripting 13 04-02-2008 01:43 AM
parse a string variable methos Shell Programming and Scripting 3 10-18-2005 01:18 PM
How to parse a string into variables aquimby Shell Programming and Scripting 3 02-22-2005 06:37 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 03-06-2007
Registered User
 

Join Date: Aug 2006
Posts: 2
String parse question

I have a string of data that looks like this:

[1] private.enterprises.954.1.1.1.1.1.2618 \(OctetString\): U [2] private.enterprises.954.1.1.1.1.2.2618 \(OctetString\): 2618

I am trying to parse the string to only return the values after the ":". Ex from above "U" and "2618".

Any suggestions?
Reply With Quote
Forum Sponsor
  #2  
Old 03-06-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
One way:
Code:
$ cat string
#! /usr/bin/ksh

string="[1] private.enterprises.954.1.1.1.1.1.2618 \(OctetString\): U [2] private.enterprises.954.1.1.1.1.2.2618 \(OctetString\): 2618"
echo string = $string
string=${string#*: }
first=${string%% *}
second=${string##*: }
echo first = $first
echo second = $second
exit 0
$ ./string
string = [1] private.enterprises.954.1.1.1.1.1.2618 \(OctetString\): U [2] private.enterprises.954.1.1.1.1.2.2618 \(OctetString\): 2618
first = U
second = 2618
Reply With Quote
  #3  
Old 03-06-2007
cfajohnson's Avatar
Registered User
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 784
Quote:
Originally Posted by mnreferee
I have a string of data that looks like this:

[1] private.enterprises.954.1.1.1.1.1.2618 \(OctetString\): U [2] private.enterprises.954.1.1.1.1.2.2618 \(OctetString\): 2618

I am trying to parse the string to only return the values after the ":". Ex from above "U" and "2618".

Any suggestions?
In any POSIX shell (bash, ksh, ash, etc.), you can use parameter expansion to extract parts of a string:

Code:
var="abc:def ghi jkj:lmn opq"
left1=${var%%:*} ## Everything to the left of the first colon
left2=${var%:*} ## Everything to the left of the rightmost colon
right1=${var##* } ## Everything to the right of the rightmost space
right2=${var#* } ## Everything to the right of the first space
Reply With Quote
  #4  
Old 03-06-2007
Registered User
 

Join Date: Aug 2006
Posts: 2
Thanks for the help. I will give it a try.
Reply With Quote
  #5  
Old 03-06-2007
dj -------
 

Join Date: Feb 2007
Location: Cochin/Bangalore, India
Posts: 418
Assuming that the variable var contains the above data

echo $var | awk -F: '{print substr($2,1,2)" " $3;}'
Reply With Quote
  #6  
Old 03-06-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,609
Code:
echo "[1] private.enterprises.954.1.1.1.1.1.2618 \(OctetString\): U [2] private.enterprises.954.1.1.1.1.2.2618 \(OctetString\): 2618" | sed 's/\(.*\): \(.*\) \[\(.*\): \(.*\)/\2\
\4/'
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0