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 reads two files 1

Status
Not open for further replies.

frangac

Technical User
Feb 8, 2004
163
ZA
Hi All,

Is there a way in which AWK reads two files, get a result from each file, and then subtracts file1 result from file2 result.
e.g.

x=/= "RM_[0-9][0-9][0-9][0-9][0-9]/{print substr($3,2,10),substr($3,7,5)}' from file1
y={if($1~/^IP/ && $9~/M_[0-9]/){print substr($9,1,10),substr($9,6,5)}}' | sort -u from file2

res=(x[2]-y[2])

then
{if(res ==3) {----> here we call a system call
}
else {if(res == 2)
{----> here we call a system call}
}
}
}

My x value is Jack_00212 00212
y value is Jack_00210 00210

Many Thanks
Chris
 
Hi All,

I think I have a solution to my question.

awk '
BEGIN {
while ( getline < "/DFG/c.log" > 0 )
if ( $1 ~ /^IMP/ && $9~/RM_[0-9]/){
p=substr($9,1,10)" "substr($9,6,5)
split(p,a," ")
print p,a[2] | "sort -u|tail -n1"
}
}
/RM_[0-9][0-9][0-9][0-9][0-9]/{
s=substr($3,2,10)" "substr($3,7,5)
split(s,b," ")
print s,b[2] |"tail -n1"
}
END{
res= b[2] - a[2];
if ( res <= 3 ){
etc
' file2

Could this be done in a different method and how? Just curious to learn.

Thanks
Chris
 
Is there a way in which AWK reads two files, get a result from each file, and then subtracts file1 result from file2 result.
Adjust for your needs.
Code:
# Remember the numbers from file 1.
NR==FNR { a[NR] = $0 ; next }

# Subtract the file1 numbers from file2 numbers.
{ print $0 - a[FNR] }
 
Hi Futurelet,

Thanks for your responce. It works (great idea), but what I expect is the final line from file1(print p,a[2] | "sort -u|tail -n1" as from my code above) and the same for file2. Thereafter use the final result (last line) from file1 and subtract from file2 (last line).

Many Thanks
Chris
 
Replace this:
print p,a[2] | "sort -u|tail -n1"
By this:
if(aMax<a[2])aMax=a[2]
This:
print s,b[2] |"tail -n1"
By this:
bLast=b[2]
And this:
res= b[2] - a[2];
By this:
res=bLast - aMax

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV,

Congrats. One question, what value is "aMax".

Thanks
Chris
 
The maximum value of a[2], to mimic the sort | tail system call.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV,

Once again many thanks and I must say this forum to me is the best.

Thanks
Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top