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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through Arrays 1

Status
Not open for further replies.

SpiritOfLennon

IS-IT--Management
Oct 2, 2001
250
GB
Hi,
I'm trying to write an awk script to create audit tables and triggers for an Oracle database but I'm having problems with my array.

Sample input is
AAAAA|BBBBB
AAAAA|CCCCC
AAAAA|DDDDD
EEEEE|FFFFF
EEEEE|GGGGG

I want my output to be
AAAAA|BBBBB|CCCCC|DDDDD
EEEEE|FFFFF|GGGGG

Could anyone make any suggestions? I have
BEGIN { FS="|" ; cnt = 0 +1 }
TABLE[NR] = $2
COLUMN[NR] = $3
FIELD[NR] = $4
LENGTH[NR] = $5
PRECISION[NR] = $6
NULL[NR] = $8
END {for(i=1;i<=NR;i++)
{ if (TABLE[i+1] != TABLE) cnt2 = i + 1
} { if(TABLE[i+1] != TABLE) {print &quot;create table &quot;TABLE&quot; (&quot;
{ for (j=cnt;j<=i;j++)
print COLUMN[j]&quot; &quot;FIELD[j]&quot;(&quot;LENGTH[j]&quot;,&quot;PRECISION[j]&quot;) &quot;NULL[j]
cnt = cnt2
} }
} }

SOL
I'm only guessing but my guess work generally works for me.
 
How about something like this which doesn't need arrays. It will need some work to match you input file but appears to do what you are suggesting.

BEGIN {FS=&quot;|&quot;
marker=&quot;!&quot;}

#print closing brace for each create table
$1 != marker && marker != &quot;!&quot; {print &quot;)&quot;}

#table name has changed, so new create table
$1 != marker {print &quot;create table &quot;$1&quot; (&quot;;marker=$1}

#same tablename as previous line, so it's a table row
{print $2&quot; &quot;$3&quot;(&quot;$4&quot;,&quot;$5&quot;) &quot;$6}

END {print &quot;)&quot;}

Hope this makes some sense

Greg.
 
Does this do what you want?
Code:
BEGIN{FS=&quot;|&quot;}
{
  if (NR>1 && hld1!=$1) {
    print &quot;&quot;
    flg = 0
  }
  if (!flg) {
    printf $0
    flg = 1
  }
  else {
    printf(&quot;|%s&quot;,$2)
  }
  hld1 = $1
}
Hope this helps. CaKiwi
 
Greg,
Yours works fine, CaKiwi I think I may be mistyping your's but I can't get it to work. Thanks both of you. SOL
I'm only guessing but my guess work generally works for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top