array and string with for-loop and grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting array and string with for-loop and grep
# 1  
Old 09-09-2011
array and string with for-loop and grep

Hello together,

first of all, iam really a beginner in Shellskripting and i need some help please. Following Task i try to finished:

vmtoolsd --cmd 'info-get guestinfo.ovfEnv' > vmt

In the file vmt are some strings that iam searching for.
For that i try to create a array like:

list=(dns1 dns2 ip gateway netmask) --> The strings that i look for are "dns1", "dns2", and so on...

now i try the following:

list=(dns1 dns2 ip gateway netmask) #is the syntax correctyl?
vmtoolsd --cmd 'info-get guestinfo.ovfEnv' > vmt

for i in $(cat vmt);
if you find "dns1" then save the value" 145.230.8.1...
so what iam trying is following: i want to look after die array values in the vmt file: If the for-loop find "dns1" or "dns2" he should save the ip for that:

the vmt file looks so:

$ cat vmt
<?xml version="1.0" encoding="UTF-8"?>
<Environment
xmlns="Index of /ovf/environment/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlnsSmiliee="Index of /ovf/environment/1"
xmlns:ve="http://www.vmware.com/schema/ovfenv"
oe:id="">
<PlatformSection>
<Kind>VMware ESXi</Kind>
<Version>4.1.0</Version>
<Vendor>VMware, Inc.</Vendor>
<Locale>de</Locale>
</PlatformSection>
<PropertySection>
<Property oe:key="dns1" oe:value="145.230.8.1"/>
<Property oe:key="dns2" oe:value="145.230.8.1"/>
<Property oe:key="gateway" oe:value="145.230.8.254"/>
<Property oe:key="ip" oe:value="145.230.8.33"/>
<Property oe:key="netmask" oe:value="255.255.255.0"/>
</PropertySection>
<ve:EthernetAdapterSection>
<ve:Adapter ve:mac="00:50:56:84:00:0b" ve:network="NLSHOP_145.230.39.128-26"/>
</ve:EthernetAdapterSection>
</Environment>

you can see the string "Property" --> there are my values like dns1 a.s.o how can i get the value for that???

i would be very happy if someone could show a solution for that Smilie((

thank you


greets

tim
# 2  
Old 09-09-2011
Instead of putting each individual line in FONT tags, just put the whole thing in one big CODE tag. That'll format it nicely for you.

Code:
list=(dns1 dns2 ip gateway netmask) #is the syntax correctyl?

Depends on your shell. It'll work in BASH.

[FONT=Times New Roman][FONT=Times New Roman]vmtoolsd --cmd 'info-get guestinfo.ovfEnv' > vmt

Code:
for i in $(cat vmt);

That's a combination useless use of backticks and useless use of cat. When the data's large enough, you might find it silently truncating data and the like. It'll also split on spaces, not just lines.

Instead of that I reccomend this method:
Code:
# The IFS="" part prevents it from chopping off leading whitespace.
while IFS="" read LINE
do
        ...
done < vmt

which won't ever truncate the data and won't run into the size limit of a shell variable.


Code:
if you find "dns1" then save the value" 145.230.8.1...

Since you appear to have BASH:

Code:
$ STR="...DNS1..."
$ KEY="DNS1"
$ VAL="145.230.8.1"
$ echo "${STR//$KEY/$VAL}"
...145.230.8.1...
$

# 3  
Old 09-09-2011
Parsing XML can be tricky but since you only want small information you can use something like this with awk:

Code:
[mute@geek ~]$ awk -F'"' '/oe:key/ {print $2 OFS $4 }' vmt
dns1 145.230.8.1
dns2 145.230.8.1
gateway 145.230.8.254
ip 145.230.8.33
netmask 255.255.255.0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk Associative Array and/or Referring to Field by String (Nonconstant String Value)

I will start with an example of what I'm trying to do and then describe how I am approaching the issue. File PS028,005 Lexeme HRS # M # PhraseType 1(1:1) 7(7) PhraseLab 501 503 ClauseType ZYq0 PS028,005 Lexeme W # L> # BNH # M #... (17 Replies)
Discussion started by: jvoot
17 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

4. Shell Programming and Scripting

Loop through array of arrays of string with spaces

Hi I'm trying to loop through an array that contains other arrays and these arrays consist of strings with spaces. The problem is that I can't seem to preserve the spacing in the string. The string with spaces are either divided into multiple items if I change IFS to \n or all the elements of... (4 Replies)
Discussion started by: kidmanos
4 Replies

5. Homework & Coursework Questions

passing letters from an array into a string for string comparison

attempting the hangman program. This was an optional assignment from the professor. I have completed the logical coding, debugging now. ##I have an array $wordString that initializes to a string of dashes ##reflecting the number of letters in $theWord ##every time the user enters a (valid)... (5 Replies)
Discussion started by: lotsofideas
5 Replies

6. Shell Programming and Scripting

Array with do while and if loop

Hi All, I am trying to run a do while for an array. And in the do while, I'm trying to get a user response. Depending on the the answer, I go ahead and do something or I move on to next element in the array. So far I can read the array, but I can't get the if statement to work. Any suggestions... (5 Replies)
Discussion started by: nitin
5 Replies

7. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

8. Shell Programming and Scripting

loop in array in python

Hi suppose in python I have a list(or array, or tuple, not sure the difference) How do I loop inside the size of array. The pseudo code is: a= for i = 1 to dim(a) print a end How to find the dimension in python? Also, anyone has a handbook to suggest so I can borrow from library (1 Reply)
Discussion started by: grossgermany
1 Replies

9. Shell Programming and Scripting

Perl grep array against array

Hi, Is there any way I can grep an array against another array? Basically here's what I need to do. There will be an array containing some fixed texts and I have to check whether some files contain these lines. Reading the same files over and over again for each different pattern doesnt seem... (1 Reply)
Discussion started by: King Nothing
1 Replies

10. UNIX for Dummies Questions & Answers

Reading from while loop into an array

Hi I have something like cat $HOME/all_dirs | while read ln_old_dirs do if then echo "$ln_all_old_dirs" fi done As you know that the variable ln_all_old_dirs is not accessable from outside the... (2 Replies)
Discussion started by: ssuresh1999
2 Replies
Login or Register to Ask a Question