Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

UNIX ksh parsing colon delimited string

Posted on 2013-06-10
9
Medium Priority
?
567 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 2000 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 2000 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
Technology Partners: 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 2000 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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.
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…
Suggested Courses

972 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