Re: How to make a loop Extracting items from string (799 Views)
Reply
Occasional Advisor
SwissKnife
Posts: 8
Registered: ‎10-21-2011
Message 1 of 10 (814 Views)
Accepted Solution

How to make a loop Extracting items from string

Hi

 

I have lines like this in a file

 

Server1 Entry1 Entry2 Entry3 ORA9|ORA10|ORA11

Server2 Entry1 Entry2 Entry3 ORA9|ORA11

Server3 Entry1 Entry2 Entry3 ORA8|ORA73|ORA11

Server4 Entry1 Entry2 Entry3 ORA8|ORA73|ORA10

...

 

I need a particular action on Entry5 where I need to manage all sub items (ORAXXX, ......) cheking if the sub item is ORA10 (ORA10 is a parameter of my script). Entry 5 is a set of value with | separator.

 

Then, How to extract all items to have an output like this

 

My servers with Oracle 10 are

Server1

Server2

 

 

 

Any idea ?

 

 

Best Regards

Den

 

Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 10 (812 Views)

Re: How to make a loop Extracting items from string

Hi:

 

Perhaps:

 

# awk '$5~/ORA10/ {print $1}' myfile
Server1
Server4

Regards!

 

...JRF...

Occasional Advisor
SwissKnife
Posts: 8
Registered: ‎10-21-2011
Message 3 of 10 (808 Views)

Re: How to make a loop Extracting items from string

Hi James

 

In the old version of the HP Forum it was possible to submit points. This is always possible ?

 

Anyway, thanks for your help.

Best Regards

Den

Occasional Advisor
SwissKnife
Posts: 8
Registered: ‎10-21-2011
Message 4 of 10 (803 Views)

Re: How to make a loop Extracting items from string

Hi James

 

In fact, this is work.

I have an internal contraint in fact.

 

I need to extract and test in a loop for each sub item

(I have simplified to much)

 

I'm able to extract the string where I have sub items. This is easy beacause the position using $5.

Well

 

in the loop I need to test like this

 grep -i subitem $AnotherString and take action depending of the result.

 

 

you see ?

 

Best regards

Den

 

Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 10 (804 Views)

Re: How to make a loop Extracting items from string


SwissKnife wrote:

Hi James

 

In the old version of the HP Forum it was possible to submit points. This is always possible ?

 

Anyway, thanks for your help.

Best Regards

Den


Hi Den:

 

I thought it might be you from your sig!

 

Yes, in this new community you give "points" with "kudos'.  One or more responses or responders can be kudoed by clicking the kudo star next to the person's name.

 

Marking a post as solved, and giving kudos are separate things (unfortunately).  See here for more information:

 

http://h30499.www3.hp.com/t5/help/faqpage/faq-category-id/kudos#kudos

 

http://h30499.www3.hp.com/t5/help/faqpage/faq-category-id/solutions#solutions

 

Regards!

 

...JRF...

Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 10 (801 Views)

Re: How to make a loop Extracting items from string


SwissKnife wrote:

I need to extract and test in a loop for each sub item

(I have simplified to much)

 

I'm able to extract the string where I have sub items. This is easy beacause the position using $5.

Well

 

in the loop I need to test like this

 grep -i subitem $AnotherString and take action depending of the result.

 


Hi (again) Den:

 

As always a description of the problem with sample input and the desired output leads to a solution the quickest.  Please provide that.

 

Regards!

 

...JRF...

Occasional Advisor
SwissKnife
Posts: 8
Registered: ‎10-21-2011
Message 7 of 10 (799 Views)

Re: How to make a loop Extracting items from string

Sorry James, Of course. Here my staff

 

I have a config file (some lines only)

servers.cfg

LAB Server1 NOALIAS  STANDALONE ORA_9|ORA_10
LAB Server2 NOALIAS  STANDALONE ORA_9|ORA_11
LAB Server3 NOALIAS  STANDALONE ORA_8|ORA_11
LAB Server4 NOALIAS  STANDALONE ORA_8|ORA_10

 

The "|" is for say: (Example) Server1 had Oracle 9 and Oracle 10 installed.

 

In my script I'm doing something like this:

CONSTRAINT="RAC1 RAC2 FAILOVER STANDALONE ORA_8 ORA11"
okServer=""

cat /home/oracle/automation/rdist.config/servers.cfg |while read line ; do

  echo$line

  if my line is matching the $CONSTRAINT then I need to add line($2) in my okServer

 

done

 

 

Finaly in my okServer variable I should have only this

echo $okServer

output will be

Server2 Server3 Server4

 

Thank you James

Best regards

Den

 

Occasional Advisor
SwissKnife
Posts: 8
Registered: ‎10-21-2011
Message 8 of 10 (797 Views)

Re: How to make a loop Extracting items from string

Yes it's me, when the site has changed I lost my account .... (sic) ;-) But I'm back !

Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 9 of 10 (788 Views)

Re: How to make a loop Extracting items from string

Hi (again) Den:

 

OK see if this meets your needs.

 

# cat ./extract
#!/bin/sh
CONSTRAINT="RAC1 RAC2 FAILOVER STANDALONE ORA_8 ORA_11"
okServer=""
export CONSTRAINT;
while read line
do
    NEW=$(echo ${line}|perl -nae '$ENV{CONSTRAINT}=~$F[4] && print "$F[1]\n"')
    [ ! -z ${NEW} ] && okServer=$(echo "${okServer} ${NEW}")
done < /home/oracle/automation/rdist.config/servers/cfg
echo "${okServer}"
exit 0

# ./extract

 Server2 Server3 Server4

 

Regards!

 

...JRF...

Occasional Advisor
SwissKnife
Posts: 8
Registered: ‎10-21-2011
Message 10 of 10 (763 Views)

Re: How to make a loop Extracting items from string

Hi James

Sorry for the delay. I'm validating your solution. I'll be back soon.

Best Regards
Den
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.