Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grep Question 2

Status
Not open for further replies.

yerra

IS-IT--Management
Jan 6, 2005
11
FR
I have a variable named ARCLOG with value ARCHIVELOG,/oradata/archives,46455433.

Now I want to evaluate a condition where
a) If value in 1st column is ARCHIVELOG, check the 2nd column for its location (/oradata/archives or any loc). if there is no value in 2nd column , then display a message and stop.
If 1st column value is NOARCHIVELOG, then stop.
These values are comma separated within the ARCLOG variable.

Require ur help..
 
Get ready for the inevitable one-line perl solution in the next few minutes. Until then, I'd recommend using awk and a simple if/elif statement.

I'm not a big fan of posting the actual code (mostly because I'm just lazy), but I'll walk you through the general idea.

First, you can break down the variable with awk. Here's a basic example of how awk works. There are may ways to do this and I can almost already see other people out there posting more efficient ways.

echo $VAR | awk -F, '{ print $1, $2, $3 }'

This would look at the contents of variable VAR, use a comma (,) as a delimiter, and show the values of the first through third values. You can always use just look at an individual value (ie. awk -F, '{ print $2 }').

Create a simple if/then statement that says if the first value equals what you're looking for then look at the second value. If this second value equals what you're looking for then perform x action.

Again, I'm being purposely vague. I find that awk is a great utility to quickly look through delimited files and variables. It's easy enough for a monkey like me to understand it.

Good luck!
 
in Perl it's sth like

my($1,$2,$3)=split(",",$ARCLOG);

and then you can test
if("$1" eq "ARCHIVELOG")
{
if ("$2" eq "oradata/archives")
{
do action whatever you want;
}
}

greetz

R.
 
Thanks for the help.. I could get it.

 
[tt]
echo $VAR | awk '
BEGIN { FS = "," }
{ if ("ARCHIVELOG" == $1)
if ("/oradata/archives" == $2 )
{ print "o.k."
## process
}
else
print "2nd column invalid."
}'
[/tt]
 
I tried your code, it works ok, but If I want to display the values other than /oradata/archives in the else part of the print statement, then how do i go about this ?

 
print "2nd column \""$2"\" invalid."

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top