Operating System - HP-UX
1745921 Members
4420 Online
108723 Solutions
New Discussion

Re: How to make a loop Extracting items from string

 
SOLVED
Go to solution
SwissKnife
Frequent Advisor

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

 

9 REPLIES 9
James R. Ferguson
Acclaimed Contributor
Solution

Re: How to make a loop Extracting items from string

Hi:

 

Perhaps:

 

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

Regards!

 

...JRF...

SwissKnife
Frequent Advisor

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

SwissKnife
Frequent Advisor

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

 

James R. Ferguson
Acclaimed Contributor

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...

James R. Ferguson
Acclaimed Contributor

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...

SwissKnife
Frequent Advisor

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

 

SwissKnife
Frequent Advisor

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 !

James R. Ferguson
Acclaimed Contributor

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...

SwissKnife
Frequent Advisor

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