Solved

UNIX ksh parsing colon delimited string

Posted on 2013-06-10
9
519 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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.:

776 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