Parse ; deliminated variable to create variables


 
Thread Tools Search this Thread
Operating Systems Linux Parse ; deliminated variable to create variables
# 1  
Old 11-17-2012
Parse ; deliminated variable to create variables

Hi there, would appreciate some help on this parsing problem if anybody can help

im trying to parse a variable with the following output, each of the values im trying to parse are deliminated by a ;

Code:
T192 globalprefs={"huddler_uid":"ad079807c1b5bca1bd05e21fd3711c1b"%2C"registered":true};5590387371d88d9e5de34c5d46b0ab8b82e7a8a90abb28a32=Bi0LdA4tBzsDOwU2BicHagA1UjcFMlBkVGdTOQM%2BX2MPbQJjAnk;%3D%3D;huddleprefs={"pagecount":41};HUDSESSID=2kul3tre4vfid0vf8cvk3f23c1;authchallenge=423c115aeaafe219533fac6d59d1b4e7;longauth=c31c%2Ff6R%2Cs7y79yh6K0ryqlzEQau%2B00lAJLljU1Jlongauth=c31c%2Ff6R%2Cs7y79yh6K0ryqlzEQau%2B00lAJLljU1;expires=Tue,15-Nov-202208:53:30GMT;path=/ huddleprefs={"pagecount":42};expires=Sun,17-Nov-201308:53:32GMT;path=/ globalprefs={"huddler_uid":"ad079807c1b5bca1bd05e21fd3711c1b"%2C"registered":true};expires=Sun,17-Nov-201308:53:32GMT;path=/;domain=

im trying to parse out both names and value's in the whole variable and store them as following

Code:
$name[1] = HUDSESSID
$value[1] = 2kul3tre4vfid0vf8cvk3f23c1
$name[2] = huddleprefs
$value[2] = {"pagecount":41}

etc... etc...

the order that the array is built does not matter, as long as I get each name and value into the appropriate array's

Can you guys point me in the correct direction in order to do this? I tried messing with eval to auto parse them but didnt have much luck getting them into an array

thanks guys

Last edited by Scrutinizer; 11-18-2012 at 09:07 AM.. Reason: extra code tags
# 2  
Old 11-17-2012
Code:
awk -F"[; ]" '{for(i=1;i<=NF;i++) {if($i~/=/){ split($i,a,"=");n[++c]=a[1];v[c]=a[2]}}}END{for(j=0;++j<=c;) print "n["j"]="n[j] "\t" "v["j"]=" v[j]}' yourfile

Code:
$ wc -l <t1
1
$ cat t1
T192 globalprefs={"huddler_uid":"ad079807c1b5bca1bd05e21fd3711c1b"%2C"registered":true};5590387371d88d9e5de34c5d46b0ab8b82e7a8a90abb28a32=Bi0LdA4tBzsDOwU2BicHagA1UjcFMlBkVGdTOQM%2BX2MPbQJjAnk;%3D%3D;huddleprefs={"pagecount":41};HUDSESSID=2kul3tre4vfid0vf8cvk3f23c1;authchallenge=423c115aeaafe219533fac6d59d1b4e7;longauth=c31c%2Ff6R%2Cs7y79yh6K0ryqlzEQau%2B00lAJLljU1Jlongauth=c31c%2Ff6R%2Cs7y79yh6K0ryqlzEQau%2B00lAJLljU1;expires=Tue,15-Nov-202208:53:30GMT;path=/ huddleprefs={"pagecount":42};expires=Sun,17-Nov-201308:53:32GMT;path=/ globalprefs={"huddler_uid":"ad079807c1b5bca1bd05e21fd3711c1b"%2C"registered":true};expires=Sun,17-Nov-201308:53:32GMT;path=/;domain=
$ awk -F"[; ]" '{for(i=1;i<=NF;i++) {if($i~/=/){ split($i,a,"=");n[++c]=a[1];v[c]=a[2]}}}END{for(j=0;++j<=c;) print "n["j"]="n[j] "\t" "v["j"]=" v[j]}' t1
n[1]=globalprefs    v[1]={"huddler_uid":"ad079807c1b5bca1bd05e21fd3711c1b"%2C"registered":true}
n[2]=5590387371d88d9e5de34c5d46b0ab8b82e7a8a90abb28a32    v[2]=Bi0LdA4tBzsDOwU2BicHagA1UjcFMlBkVGdTOQM%2BX2MPbQJjAnk
n[3]=huddleprefs    v[3]={"pagecount":41}
n[4]=HUDSESSID    v[4]=2kul3tre4vfid0vf8cvk3f23c1
n[5]=authchallenge    v[5]=423c115aeaafe219533fac6d59d1b4e7
n[6]=longauth    v[6]=c31c%2Ff6R%2Cs7y79yh6K0ryqlzEQau%2B00lAJLljU1Jlongauth
n[7]=expires    v[7]=Tue,15-Nov-202208:53:30GMT
n[8]=path    v[8]=/
n[9]=huddleprefs    v[9]={"pagecount":42}
n[10]=expires    v[10]=Sun,17-Nov-201308:53:32GMT
n[11]=path    v[11]=/
n[12]=globalprefs    v[12]={"huddler_uid":"ad079807c1b5bca1bd05e21fd3711c1b"%2C"registered":true}
n[13]=expires    v[13]=Sun,17-Nov-201308:53:32GMT
n[14]=path    v[14]=/
n[15]=domain    v[15]=
$

---------- Post updated at 12:25 PM ---------- Previous update was at 12:10 PM ----------

But you could also :

1) Setup you own environment file :
Code:
awk '/=/' RS=";" myfile >myvar.env

2) Load it :
Code:
. ./myvar.env

You can then call your variables directly by using their name.

Last edited by ctsgnb; 11-17-2012 at 07:16 AM..
# 3  
Old 11-17-2012
what if i want to do this from a variable holding that information rather than a file
# 4  
Old 11-17-2012
I guess you want the names and values in shell variables, too. Try this bash approach, assuming VAR1 holds your string. You may need to refine yourself (as you did not specify further details) to eliminate unneeded parameters:
Code:
echo $VAR1|
sed 's/;[^; ]* /;/g;s/;/;\n/g' |
{ IFS="="; while read name[$((++i))] value[$i]; do echo -en "name[$i]= "${name[$i]} ",\tvalue[$i]= " ${value[$i]} "\n"; done; }
name[1]= T192 globalprefs ,    value[1]=  {"huddler_uid":"ad079807c1b5bca1bd05e21fd3711c1b"%2C"registered":true}; 
name[2]= 5590387371d88d9e5de34c5d46b0ab8b82e7a8a90abb28a32 ,    value[2]=  Bi0LdA4tBzsDOwU2BicHagA1UjcFMlBkVGdTOQM%2BX2MPbQJjAnk; 
name[3]= %3D%3D; ,             value[3]=  
name[4]= huddleprefs ,         value[4]=  {"pagecount":41}; 
name[5]= HUDSESSID ,           value[5]=  2kul3tre4vfid0vf8cvk3f23c1; 
name[6]= authchallenge ,       value[6]=  423c115aeaafe219533fac6d59d1b4e7; 
name[7]= longauth ,            value[7]=  c31c%2Ff6R%2Cs7y79yh6K0ryqlzEQau%2B00lAJLljU1Jlongauth c31c%2Ff6R%2Cs7y79yh6K0ryqlzEQau%2B00lAJLljU1; 
name[8]= expires ,             value[8]=  Tue,15-Nov-202208:53:30GMT; 
name[9]= huddleprefs ,         value[9]=  {"pagecount":42}; 
name[10]= expires ,            value[10]=  Sun,17-Nov-201308:53:32GMT; 
name[11]= globalprefs ,        value[11]=  {"huddler_uid":"ad079807c1b5bca1bd05e21fd3711c1b"%2C"registered":true}; 
name[12]= expires ,            value[12]=  Sun,17-Nov-201308:53:32GMT; 
name[13]= path ,               value[13]=  /; 
name[14]= domain ,             value[14]=

# 5  
Old 11-17-2012
I get this error on the command you specified

line 21: =name[1]=: command not found

---------- Post updated at 11:48 AM ---------- Previous update was at 11:42 AM ----------

actually never mind sorry, i got it

---------- Post updated at 01:12 PM ---------- Previous update was at 11:48 AM ----------

yeah that works great actually, currently trying to edit your code so that I dont add in duplicates, is there an easy way to do this?
# 6  
Old 11-17-2012
Code:
# set -- $(awk 'NR==1{sub($1,z)}gsub("="," ")' RS=";" yourfile)
# for i in `seq 1 $#` ; do eval echo "'$'{$i}="'$'{$i};done
${1}=globalprefs
${2}={"huddler_uid":"ad079807c1b5bca1bd05e21fd3711c1b"%2C"registered":true}
${3}=5590387371d88d9e5de34c5d46b0ab8b82e7a8a90abb28a32
${4}=Bi0LdA4tBzsDOwU2BicHagA1UjcFMlBkVGdTOQM%2BX2MPbQJjAnk
${5}=huddleprefs
${6}={"pagecount":41}
${7}=HUDSESSID
${8}=2kul3tre4vfid0vf8cvk3f23c1
${9}=authchallenge
${10}=423c115aeaafe219533fac6d59d1b4e7
${11}=longauth
${12}=c31c%2Ff6R%2Cs7y79yh6K0ryqlzEQau%2B00lAJLljU1Jlongauth
${13}=c31c%2Ff6R%2Cs7y79yh6K0ryqlzEQau%2B00lAJLljU1
${14}=expires
${15}=Tue,15-Nov-202208:53:30GMT
${16}=path
${17}=/
${18}=huddleprefs
${19}={"pagecount":42}
${20}=expires
${21}=Sun,17-Nov-201308:53:32GMT
${22}=path
${23}=/
${24}=globalprefs
${25}={"huddler_uid":"ad079807c1b5bca1bd05e21fd3711c1b"%2C"registered":true}
${26}=expires
${27}=Sun,17-Nov-201308:53:32GMT
${28}=path
${29}=/
${30}=domain

So that name='$'i (where i%2=1) and value='$'((i+1))
# 7  
Old 11-17-2012
thanks very much for the reply once again, but it seems the last code u gave is for reading from a file, and also there seems to be duplicate names in the list?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grepping for one variable while using awk to parse an associated variable

Im trying to search for a single variable in the first field and from that output use awk to extract out the lines that contain a value less than a value stored in another variable. Both the variables are associated with each other. Any guidance is appreciated. File that contains the... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

2. Shell Programming and Scripting

awk to create variables to pass into a bash loop to create a download link

I have created one file that contains all the necessary info in it to create a download link. In each of the lines /results/analysis/output/Home/Auto_user_S5-00580-6-Medexome_67_032/plugin_out/FileExporter_out.67... (8 Replies)
Discussion started by: cmccabe
8 Replies

3. Shell Programming and Scripting

Parse variables from C++ to shell

Hi All, #include <iostream> int main() { std::int foo = 34; system("mkdir /home/linuxUser/fooDir"); system("vi fooFile") system("write foo in fooFile") foo = 43; foo = read foo from fooFile; std::cout << "foo = " << foo ; } result should be foo = 34 can... (3 Replies)
Discussion started by: linuxUser_
3 Replies

4. UNIX for Dummies Questions & Answers

Parse or cut concat variables to individual values

Hello I need to pass some environment parameters to a datastage job and am getting an error when trying to send the complete concatinated variable. I have decided to parse out just the values and send as parameters but am struggling to find the best way to do this (actually I am not very... (3 Replies)
Discussion started by: LynnC
3 Replies

5. Shell Programming and Scripting

Parse config file and store the values in variables

Hi, I have a config file that has blank, commented lines. I need to escape commented lines, blank lines, parse the remaining lines and store them in variables or array. the config file contains the following lines. # config file # Define Oracle User ORA_USER=abcde ORA_PASS=xyzabc... (8 Replies)
Discussion started by: Lakshmi Chowdam
8 Replies

6. Shell Programming and Scripting

Want to parse output for variables in Bash

Trying to finish up my script that automates some video encoding work. Situation: There is an MKV file to be transcoded. Problem: MKVINFO will give a bunch of output about an MKV file, included in that output are two lines I am interested in: | + Default duration: 41.708ms (23.976 fps... (9 Replies)
Discussion started by: randyharris
9 Replies

7. Shell Programming and Scripting

How to parse a string into variables

I'm working in korn shell and have a variable which contains a string like: aa_yyyymmdd_bbb_ccc_ddd.abc. I want to treat the _ and . as delimiters and parse the string so I end up with 6 values in variables that I can manipulate. My original plan was to use var1=`echo $sting1 | cut -c1-c2` but... (9 Replies)
Discussion started by: aquimby
9 Replies

8. Shell Programming and Scripting

How to: Parse text string into variables using Korn shell

I am writing a script to keep check on free disk space, and I would like to find a way to parse $LINE (see code below) into a numeric value (for free disk space percentage) and a string value (for mount point). If possible, I would like to avoid sed or any additional use of awk since I am not very... (7 Replies)
Discussion started by: shew01
7 Replies

9. Shell Programming and Scripting

How can I parse a record found in /etc/passwd into variables?

I am working with the Oracle 10.2.0.3 job scheduler on Solaris 10, and unfortunately, the scheduler executes scripts in such a way that several default shell environment variables are not defined. For example, $HOME, $USER, and $LOGNAME are missing. How can I parse the appropriate record in... (7 Replies)
Discussion started by: shew01
7 Replies

10. UNIX for Dummies Questions & Answers

convert file deliminated by colon to one deliminated by tab characters

I know this seems simple to some of you so could you help? example> file:Mine and yours :daily need this deliminated to tab characters (2 Replies)
Discussion started by: yammer
2 Replies
Login or Register to Ask a Question