Operating System - HP-UX
1752325 Members
5157 Online
108786 Solutions
New Discussion юеВ

Re: use multiple field seperators with awk

 
renarios
Trusted Contributor

use multiple field seperators with awk

Hi all,

In a testcase, my /etc/oratab looks like this:
# Oracle 8 stuff (ocho)
tst1:/mnt/u01/app/oracle/product/8.1.7:Y
# Oracle 9 stuff (el nino)
tst2:/mnt/u01/app/oracle/product/9.2.0:Y
tst3:/mnt/u01/app/oracle/product/9.2.0:N

for a backup script I need all "active" Oracle 9i databases. I came up with this:
DBLIST=$(awk -F: '$1 !~ /^#/ && $3 ~ /Y$/ {print $1}' ${ORATAB})
, but that does only filter the "Y" and comment lines. How can I filter the "9" (and 10 later on) out of the lines?
Best would be use something with >8

I'm struggling with split, but I'm stuck
Any ideas?

Cheerio,

Renarios
Nothing is more successfull as failure
7 REPLIES 7
Sudeesh
Respected Contributor

Re: use multiple field seperators with awk

Hello,

I just did what you have said....

[rx260-11]/sudhi >cat kk
# Oracle 8 stuff (ocho)
tst1:/mnt/u01/app/oracle/product/8.1.7:Y
# Oracle 9 stuff (el nino)
tst2:/mnt/u01/app/oracle/product/9.2.0:Y
tst3:/mnt/u01/app/oracle/product/9.2.0:N

[rx260-11]/sudhi >awk -F: '$1 !~ /^#/ && $3 ~ /Y$/ {print $1}' kk
tst1
tst2
[rx260-11]/sudhi >


and see only test1,test2 in the o/p

what you want to achive? am I missing something in your question?


Sudeesh

The most predictable thing in life is its unpredictability
renarios
Trusted Contributor

Re: use multiple field seperators with awk

Hi Sudeesh,

Thanks for the quick response.
The output should be only tst2, because tst1 is 8.1.7 and I only want the active 9.2.0 (or in future higher) databases.

thanks,

Ren├Г
Nothing is more successfull as failure
renarios
Trusted Contributor

Re: use multiple field seperators with awk

Latest code is
awk -F: '$1 !~ /^#/ && $3 ~ /Y$/ && split($0, arr, "/") {if ( arr[7] > 8 ) print arr[7] "*" $1 " " $2 " " $3}' ${ORATAB}
, but it does not work.

Has anyone other/ideas ideas?

Cheers,

Renarios
Nothing is more successfull as failure
renarios
Trusted Contributor

Re: use multiple field seperators with awk

I found a solution:
awk -F: '$1 !~ /^#/ && $3 ~ /Y$/ && split($0, arr, "/") && split(arr[7], oraver, ".") {if ( oraver[1] >= 9 ) print $1}' ${ORATAB}

10 point for Renarios?

Only thing is: How do I get this readable?
If I use BEGIN and END, it doesn't work anymore.
Anyone ideas?

Cheerio,

Renarios
Nothing is more successfull as failure
Alex Lavrov.
Honored Contributor

Re: use multiple field seperators with awk

Dont' make it readable, if it works, leave it as is and make a really good comment how it works.

Because when you try to rewrite things so they'll look simplier, you can put there some bugs.

Good comment on this line will be enough I think :)
I don't give a damn for a man that can only spell a word one way. (M. Twain)
curt larson_1
Honored Contributor

Re: use multiple field seperators with awk

something a little more readable

awk -F: '$1 !~ /^#/ && $3 ~ /Y$/ {
split($0, arr, "/");
split(arr[7], oraver, ".");
if ( oraver[1] >= 9 ) print $1;
}' ${ORATAB}

renarios
Trusted Contributor

Re: use multiple field seperators with awk

Thanks guys,

That should do the trick.
Time for weekend. Have one on me.

Solution:
DBLIST=$( awk -F: '$1 !~ /^#/ && $3 ~ /Y$/ {
split($0, arr, "/");
split(arr[7], oraver, ".");
if ( oraver[1] >= 9 ) print $1;
}' ${ORATAB} )

Cheerio,

Renarios
Nothing is more successfull as failure