How to parse nested variable


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to parse nested variable
# 1  
Old 03-29-2011
How to parse nested variable

Hi,

I want to parse nested variable in my script.
for exp-
c=1
G1='0318'
G2='0023'
G3='3092'
G4='0014'

while [ $c -le 4 ];do
g=G$c
a=$g
echo "Group=$g and value=$a"
c=`expr $c + 1`
done

final output are as -
---------------------------
Group=G1 and value=G1
Group=G2 and value=G2
Group=G3 and value=G3
Group=G4 and value=G4

while i need output as -
----------------------------
Group=G1 and value=0318
Group=G2 and value=0023
Group=G3 and value=3092
Group=G4 and value=0014

any help is appreciated.

Thanks
Amit
# 2  
Old 03-29-2011
try to change
Code:
echo "Group=$g and value=$a"

with
Code:
echo "Group=$g and value=$(eval echo \$$a)"

Or

your while loop can be replaced with smtng like
Code:
for i in G1 G2 G3 G4
do
echo "Group=$i and value=$(eval echo \$$i)"
done

This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 03-29-2011
1.txt
G1='0318'
G2='0023'
G3='3092'
G4='0014'

Code:
#!/bin/sh

C=1

while read LINE
do
if [ $C -le 4 ]
then
   G="G$C"
   A=`echo $LINE | awk '{print substr($0,5,4)}'`
   echo "Group=$G and value=$A"
   C=`expr $C + 1`
fi
done < 1.txt

# 4  
Old 03-30-2011
Hi,

Thanks for your valuable help, it solved my problem.

thanks
Amit
# 5  
Old 03-30-2011
Anytime.Smilie
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

How to add second variable in awk nested if condition?

Hi Gurus, I have a command to assign value based on input value. current condition is "if pattern matches "case", then assign "HOLD" else "SUCC"right now, I need to add one more condition (variable name is VAR). the condition is "if pattern1 matches "case", then assign "HOLD" else if... (2 Replies)
Discussion started by: ken6503
2 Replies

3. Shell Programming and Scripting

Nested variable allocation

I've made a number of errors with this and am trying to work a solution within the same framework. /bin/ksh for host_name in ahost1 ahost2 bhost1 bhost2 do for host_prefix in a b do if echo ${host_name} | grep -qi ${host_prefix} then if ... (1 Reply)
Discussion started by: squrcles
1 Replies

4. Shell Programming and Scripting

Using shell command need to parse multiple nested tag value of a XML file

I have this XML file - <gp> <mms>1110012</mms> <tg>988</tg> <mm>LongTime</mm> <lv> <lkid>StartEle=ONE, Desti = Motion</lkid> <kk>12</kk> </lv> <lv> <lkid>StartEle=ONE, Source = Velocity</lkid> <kk>2</kk> </lv> <lv> ... (3 Replies)
Discussion started by: NeedASolution
3 Replies

5. Shell Programming and Scripting

big xml file with nested loop parse

I have an xml file with the structure: <tag1> <value1>xyx</value1> <value2>123</value2> </tag1> <tag1> <value1>568</value1> <value2>zzzzz</value2> </tag1> where I want to parse each data pair in the this single file, so something like: find first tag1 data pair... (1 Reply)
Discussion started by: unclecameron
1 Replies

6. Shell Programming and Scripting

nested double quota and white space inside variable

I have a question about nested double quotes. Any help is appreciated. Here are my commands on Mac OS. # string="Ethernet \"USB Ethernet\" \"Bluetooth DUN\" AirPort FireWire \"Bluetooth PAN\"" # echo $string Ethernet "USB Ethernet" "Bluetooth DUN" AirPort FireWire "Bluetooth PAN" #... (3 Replies)
Discussion started by: lindazhou
3 Replies

7. Shell Programming and Scripting

Enviornment Variable in B shell (I call it nested variable)

#!/bin/sh APP_ROOT_MODE1=/opt/app1.0 APP_ROOT_MODE2=/opt/app2.0 APP_ROOT=${APP_ROOT_${APP_MODE}} # enviornment variable APP_MODE will be exported in the terminal where # we run the applciation, its value is string - MODE1 or MODE2 # My intension is: # when export APP_MODE=MODE1... (4 Replies)
Discussion started by: princelinux
4 Replies

8. Shell Programming and Scripting

how to parse value of the variable

I have a variable which has a full path to the file, for example : A=/t1/bin/f410pdb Does anybody know the command to parce this variable and assign the result to 3 other variables so each subdirectory name will be in a new variable like this B=t1 C=bin D=f410pdb Many thanks -A (5 Replies)
Discussion started by: aoussenko
5 Replies

9. Shell Programming and Scripting

Variable in While Loop Nested If

Hi all, I'm having problems with the setting a variable in a nested if statement. It doesn't seem to change even if it mets the 'if' condition. My script essentially looks for a user name from the output from a kerberos command. When I find the user name, I tried to change a variable and exit... (6 Replies)
Discussion started by: geass
6 Replies

10. Shell Programming and Scripting

parse variable

I have a variable (it is a date actually -> 2007-01-03) which would be passed in as parameter, what I want is to parse in and put year, month, and day in separate variables, I have tried the following but doesn't work echo $dt | awk -F- '{print $1 $2 $3}' | read y m d Thanks in... (2 Replies)
Discussion started by: mpang_
2 Replies
Login or Register to Ask a Question