Solved

UNIX ksh parsing colon delimited string

Posted on 2013-06-10
9
508 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
  • 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now