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!

Awk search output into an array or assign to a variable 2

Status
Not open for further replies.

hammers1

Technical User
Feb 8, 2004
8
GB
Hello

I have a input file called xaa, i want to extract the lines between if & fi including if & fi from the input file and assign each group of lines that match that search pattern to an array or variable.

The idea is so I can search a group of scripts and find all the instances of if & fi condition, return the script name and assign each group of lines to a variable or an array, which is every better

So far all i can get is the output of the search, I am a bit stumped on what to do next or the best way to go about this. Any help or pointers anyone can give me , would be much appreciated.

>awk -f 6.awk xaa
if [ -z $TOP ]
then
echo " TOP empty"
fi
if
34
fi
-=-=-=-


ideal output, something on these lines
-=-=-=-=-=-=
<script name> two instances of if & fi found

instance1
if [ -z $TOP ]
then
echo &quot; TOP empty&quot;
fi

instance 2
if
34
fi

-=-=-=-=-=-=-=-

# xaa input file or any script that contains if & fi statements

if [ -z $TOP ]
then
echo &quot; TOP empty&quot;
fi

if
1 2 3
4 5 6

when
if
then
stop


if
34
fi

if fi
-=-=-=-=

6.awk file
=-=-=-==
#nawk -f 6.awk xaa > out.xaa
BEGIN {
FS=&quot;\$&quot;
}
#awk

/^if/{t=0}
{a[++t]=$0}

/^fi/{for(i=1;i<=t;++i)print a}
-=-=-=-=-=-=
 
This may get you started.

/^if/{n++;t=1}
t{a[n,t]=$0;t++}
/^fi/{b[n]=t;t=0}
END{
print FILENAME,&quot;has&quot;,n,&quot;instances&quot;
for (j=1;j<=n;j++) {
print &quot;instance &quot; j
for(k=1;k<b[j];k++)print a[j, k]
}
}

CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Hello CaKiwi
Your reply is just what I was looking for. Thanks for you help. One last question if you don't mind.

I have tried to do the folowing
nawk -v st=if -v ed=fi -f 7.awk xaa (from the command line)
but can't get that to work, does the awk syntax behave differently when you use variables in awk? how would i do this within a script, sorry taht was two questions..

/^st/{n++;t=1}
#/^if/{n++;t=1}

t{a[n,t]=$0;t++}

/^ed/{b[n]=t;t=0}
#/^fi/{b[n]=t;t=0}

END{
print FILENAME,&quot;has&quot;,n,&quot;instances&quot;
for (j=1;j<=n;j++) {
print &quot;instance &quot; j
for(k=1;k<b[j];k++)print a[j, k]
}
}
 
Try this:
Code:
$1~&quot;^&quot;st{n++;t=1}
t{a[n,t]=$0;t++}
$1~&quot;^&quot;ed{b[n]=t;t=0}
END{
  print FILENAME,&quot;has&quot;,n,&quot;instances&quot;
  for (j=1;j<=n;j++) {
    print &quot;instance &quot; j
      for(k=1;k<b[j];k++)print a[j, k]
  }
}
Don't be confused between variables and litterals.

Hope This Help
PH.
 
PHV is right. A regular expression inside / / is treated as a literal. You must use the match operator ~ for variables.

CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top