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!

Read three lines at a time

Status
Not open for further replies.

yilei

Programmer
Sep 12, 2001
1
US
I here have a simple input file
the first line is i/u, i stands for intersect and u stands for union
and the following two lines are numbers like this
2 3 5
4 6 7

if the first line is i
i
2 3 5
4 5 7
my output is suppose to look like this: 5
if the first line is u
u
2 3 5
4 5 7
my ouput is supposed to look like this: 2 3 4 5 7
(either sorted or not)

my questions is how to read the second and third lines at
the same time and compare it base on the conditon of the first line.
this is what i have so far
i know it is not right....

{
if($1=="i"){
print $0;
for(i=0;i<length($0);i++){
m=$i;
}
for(j=0;j<i;j++){
/m/ print $0;
}
}
#else print $0;
}


please help me!!!!!! thanks a lot...
 
Hi Yilei,

This is one way to do it. It is not sorted but you could add a simple bubble sort before printing your results.

# awk program to find intersections and unions.
/i/ {
getline
h1[1] = $1
h1[2] = $2
h1[3] = $3
getline
h2[1] = $1
h2[2] = $2
h2[3] = $3
j = 0
for (k2=1;k2<=3;k2++) {
for (k1=1;k1<=3;k1++) {
if (h1[k1] == h2[k2]) break
}
if (k1 <= 3) h3[++j] = h2[k2]
}
for (k1=1;k1<=j;k1++) printf(&quot;%d &quot;,h3[k1])
print &quot;&quot;
}
/u/ {
getline
h1[1] = $1
h1[2] = $2
h1[3] = $3
getline
h2[1] = $1
h2[2] = $2
h2[3] = $3
j = 3
for (k2=1;k2<=3;k2++) {
for (k1=1;k1<=3;k1++) {
if (h1[k1] == h2[k2]) break
}
if (k1 > 3) h1[++j] = h2[k2]
}
for (k1=1;k1<=j;k1++) printf(&quot;%d &quot;,h1[k1])
print &quot;&quot;
}

Hope this helps.

CaKiwi.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top