Solved

UNIX ksh parsing colon delimited string

Posted on 2013-06-10
9
542 Views
Last Modified: 2013-06-11
I have a colon delimited string and I need to split it on the colon delimiter and assign to three different variables. I know there are several ways to do that using ksh scripting but what would be the most efficient way?

My string is "123:45/67/89/321:8765"

As a result I need 3 strings formed, and second string should only consist of the first 3 parts of "45/67/89/321", a.k.a "456789". So the final result should be as follows: str1="123", str2="456789", str3="8765".
0
Comment
Question by:YZlat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39235600
STR="123:45/67/89/321:8765"

str1=${STR%%:*}
str2=$(echo $STR |awk -F":|/" '{print $2 $3 $4}')
str3=${STR##*:}

If you are on Solaris:

str1=${STR%%:*}
str2=$(echo $STR | /usr/xpg4/bin/awk -F":|/" '{print $2 $3 $4}')
str3=${STR##*:}

Or:

echo $STR | awk -F":|/" '{print $1,$2 $3 $4, $NF}' | read str1 str2 str3

If you are on Solaris:

echo $STR | /usr/xpg4/bin/awk -F":|/" '{print $1,$2 $3 $4, $NF}' | read str1 str2 str3

In any case, use

echo $str1
echo $str2
echo $str3

to verify the result.
0
 
LVL 35

Author Comment

by:YZlat
ID: 39238014
what does this part do?

 '{print $1,$2 $3 $4, $NF}'

or   '{print $2 $3 $4}')
?

And also I need to modify the output slightly:

STR="123:/45/67/89/321:8765"


echo $STR | awk -F":|/" '{print $1,$2 $3 $4, $NF}' | read str1 str2 str3


From the above I need

str1=123
str2=/45/67/89
and

str3=/45/67/89/321
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 500 total points
ID: 39238073
Those are "awk" subcommands to print the content of the indicated fields of the input string.

A comma between the field specifiers ("$1,$2") creates a space between the output fields, a space ("$2 $3") concatenates the output fields.

"NF" is an internal variable of "awk" containing the number of input fields, thus "$NF" is the content of the last input field.

How can awk know how to separate the input fields?

-F":|/"

defines the input field separators. It's a regular expression, where "|" means "or",
so we're telling awk to assume a field boundary when recognizing a colon ":" or a dash "/".
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 500 total points
ID: 39238112
STR="123:/45/67/89/321:8765"

echo $STR |awk -F":" '{print $1, $2}' | read str1 str3
str2=${str3%/*}

echo $str1
echo $str2
echo $str3
0
 
LVL 35

Author Comment

by:YZlat
ID: 39238922
this code gives me an error:

str1, str3: This is not an identifier.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39238947
Why did you put a comma inbetween?
0
 
LVL 35

Author Comment

by:YZlat
ID: 39238997
nevermind, my bad

One last thing:

STR="123:/45/67/89/321:8765"
echo $STR |awk -F":" '{print $1, $2}' | read str1 str3
str2=${str3%/*}

the above puts :/45/67/89 into str2  and in case requirement changes and I will need only two parts, i.e :/45/67. How should I modify the code?
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 500 total points
ID: 39239102
>> the above puts :/45/67/89 into str2  <<

That's not true. You won't see a colon in front!

But anyway, if you have

str3 as "/45/67/89/321"

(that's the result of my last "awk" suggestion), then, also as suggested

str2=${str3%/*}

will give str2 as "/45/67/89"

but

str2=${str3%/*/*}

will give str2 as "/45/67"

and

str2=${str3%/*/*/*}

will give str2 as "/45"

Please note the added "/*" characters!
0
 
LVL 35

Author Closing Comment

by:YZlat
ID: 39239153
Thank you for all your help!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question