Solved

UNIX ksh parsing colon delimited string

Posted on 2013-06-10
9
548 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

617 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