The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
split string using separetor rinku Shell Programming and Scripting 10 06-16-2009 02:01 PM
[KSH] Split string into array piooooter Shell Programming and Scripting 3 09-01-2007 12:22 PM
split string help senthilk615 Shell Programming and Scripting 4 03-27-2006 06:43 PM
split a string gazingdown Shell Programming and Scripting 3 02-09-2006 05:34 AM
split a file at a specified string jpl35 Shell Programming and Scripting 6 07-04-2002 11:41 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-23-2006
drd_2b drd_2b is offline
Registered User
  
 

Join Date: Apr 2006
Location: Raleigh,NC
Posts: 3
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 (permalink)  
Old 04-23-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
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 04:35 PM..
  #3 (permalink)  
Old 04-23-2006
drd_2b drd_2b is offline
Registered User
  
 

Join Date: Apr 2006
Location: Raleigh,NC
Posts: 3
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 (permalink)  
Old 04-23-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
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 (permalink)  
Old 04-23-2006
drd_2b drd_2b is offline
Registered User
  
 

Join Date: Apr 2006
Location: Raleigh,NC
Posts: 3
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 (permalink)  
Old 04-23-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Congratulations!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:18 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0