KSH split string into variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH split string into variables
# 1  
Old 04-23-2006
KSH split string into variables

Hello,
I am an intermediate scripter. I can usually find and adapt what I need by searching through previous postings, but I'm stumped.

I have a string with the format "{Name1 Release1 Type1 Parent1} {Name2 Release2 Type2 Parent2}". It is being passed as an argument into a ksh script. I need to split this string into variables such as:
Pkg1Name="Name1"
Pkg1Release="Release1"
Pkg1Type="Type1"
Pkg1Parent="Parent1"
Pkg2Name="Name2"
Pkg2Release="Release2"
Pkg2Type="Type2"
Pkg2Parent="Parent2"

Does anyone have any ideas how this may be accomplished, please?
Thanks!
D
# 2  
Old 04-23-2006
echo "{Name1 Release1 Type1 Parent1} {Name2 Release2 Type2 Parent2}" | nawk -f drd.awk

drd.awk:
Code:
BEGIN {
  FS="[{}]"
  nt=split("Pkg%dName Pkg%dRelease Pkg%dType Pkg%dParent", tmplA, " ")
}
{
  for(i=2; i<=NF; i+=2) {
    n=split($i, a, " ")
    for(j=1; j<=n; j++)
      printf("%s=\"%s\"\n", sprintf(tmplA[j], i/2), a[j])
  }
}


Last edited by vgersh99; 04-23-2006 at 05:35 PM..
# 3  
Old 04-23-2006
Thanks. Sorry I wasn't clear. This parses the string like I want, but I need the variables to be available to the ksh script for processing later.
# 4  
Old 04-23-2006
Quote:
Originally Posted by drd_2b
Thanks. Sorry I wasn't clear. This parses the string like I want, but I need the variables to be available to the ksh script for processing later.
Code:
#!/bin/ksh

$(echo "{Name1 Release1 Type1 Parent1} {Name2 Release2 Type2 Parent2}" | nawk -f drd.awk)

# 5  
Old 04-23-2006
Excellent! That worked.
I also had to add "export" to the printf statement and removed the quotes surrounding the variable values. So my .awk file ended up:

BEGIN {
FS="[{}]"
nt=split("Pkg%dName Pkg%dRelease Pkg%dType Pkg%dParent", tmplA, " ")
}
{
for(i=2; i<=NF; i+=2) {
n=split($i, a, " ")
for(j=1; j<=n; j++)
printf("export %s=%s\n", sprintf(tmplA[j], i/2), a[j])
}

Thanks so much for your prompt help!
D
# 6  
Old 04-23-2006
Congratulations!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh String Manipulation - removing variables from within a variable

Hi. I'd like to remove all values in a string variable that also exist in a second variable. What is the appropriate approach to take here? I can use a 'For' loop and check each element and then populate a new string. But is there a cleaner, simpler way? E.g. I have the following 2 variables ... (19 Replies)
Discussion started by: user052009
19 Replies

2. Shell Programming and Scripting

ksh : split hex number group

Hi, sry for poor english I have a group of hex number as : 4D40:4D42 I want so split this group in a list as : 4D40,4D41,4D42 i don't know how i can do this in ksh Thanks (5 Replies)
Discussion started by: jocazh
5 Replies

3. Shell Programming and Scripting

KSH: Split String into smaller substrings based on count

KSH HP-SOL-Lin Cannot use xAWK I have several strings that are quite long and i want to break them down into smaller substrings. What I have String = "word1 word2 word3 word4 .....wordx" What I want String1="word1 word2" String2="word 3 word4" String3="word4 word5" Stringx="wordx... (5 Replies)
Discussion started by: nitrobass24
5 Replies

4. Shell Programming and Scripting

How to split the sql output into two different variables

Hi, How to set as variable from sql output. Query: select aa.serial, ao.name from ann_amit aa JOIN ann_object ao on (aa.classid=ao.classid); I got two values from aa.serial and ao.name, I wanna make two different variable for aa.serial and ao.name. The value of aa.serial should be in... (2 Replies)
Discussion started by: KarthikPS
2 Replies

5. UNIX for Dummies Questions & Answers

Formating variables in KSH

Hi Friends , I want to know how to format the output for the following: i searched in the forum and couldnt get the exact requirement. Thanks in advance . (2 Replies)
Discussion started by: i150371485
2 Replies

6. Shell Programming and Scripting

KSH script for split a txt file

I have a problem which I would like to solve by using UNIX power and inspired minds around world. Here is the problem I have a text file and it has data as follows 1X.....................1234567890123456789T1234598765XT1 (header) 1Z01............(sub HEADER) P100001............ Q1........... (4 Replies)
Discussion started by: ask.chowhan
4 Replies

7. Shell Programming and Scripting

Split lines in a file and store them in variables

Hi, I have a file called files.txt which contains data like http://abc.xyz.com/ghi/klm/nop/qrs/tuv/wxyz/ There are multiple lines like the above in this file. In .sh script, I would like to read this file, split each line, and store the variable of "qrs" and "wxyz" in separate variables. ... (3 Replies)
Discussion started by: archana.n
3 Replies

8. Shell Programming and Scripting

Bash:How to split one string variable in two variables?

Hello, I have a paramter $param consisting just of two literals and want to split it into two parameters, so I can combine it to a new parameter <char1><string><char2>, but the following code didn't work: tmp_PARAM_1=cut -c1 $PARAM tmp_PARAM_2=cut -c2 $PARAM... (2 Replies)
Discussion started by: ABE2202
2 Replies

9. Shell Programming and Scripting

[KSH] Split string into array

Hi, Is there any way to convert a string into an array in KSH? In other words I want to split the string like this: STRING="one two three four" into an array of 4 values splitting on white space. The array should be similar to the one that would be created with the following command: ... (3 Replies)
Discussion started by: piooooter
3 Replies

10. Shell Programming and Scripting

variables in ksh

I'm new to unix scripting. How would I go about pulling the first 3 characters from a variable in ksh and storing in another variable? Thanks. (9 Replies)
Discussion started by: steve6368
9 Replies
Login or Register to Ask a Question