![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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? |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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
|
|
#3
|
||||
|
||||
|
Quote:
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
|
|
#4
|
|||
|
|||
|
Thanks for the help. I will give it a try.
|
|
#5
|
|||
|
|||
|
Assuming that the variable var contains the above data
echo $var | awk -F: '{print substr($2,1,2)" " $3;}' |
|
#6
|
|||
|
|||
|
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/' |
|||
| Google The UNIX and Linux Forums |