Operating System - HP-UX
1753515 Members
6089 Online
108795 Solutions
New Discussion юеВ

Re: Can AWK read two files

 
Chris Frangandonis
Regular Advisor

Can AWK read two files

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) | "tail -n1"}' 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 values is "Jack_00212 00212"
y values is "Jack_00210 00210"

Many Thanks
Chris
6 REPLIES 6
Steven E. Protter
Exalted Contributor

Re: Can AWK read two files

General suggestion.

I usually feed data into awk.

| awk ...

Surely you can run do this:

$result1=( | awk ... )

$result2=( | awk ... )

Then you can compare (subtract) the values in result1 and result2 variables.

You could also redirect the output to files and use the diff command to compare the results.

awk is merely a tool here. I don't think it can do the job itself, the job will get done with some thoughtful script design.

I'd try and break it into smaller chunks.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Chris Frangandonis
Regular Advisor

Re: Can AWK read two files

Hi Steven,

Thanks for your reply. Yes it was my initial plan, but I thought, why not learn from asking. I have seen AWK scripts doing this kind of method.

Many thanks
Chris
Scot Bean
Honored Contributor

Re: Can AWK read two files

Any reason you have to use awk?

Perl could do it, or consider sed.
curt larson_1
Honored Contributor

Re: Can AWK read two files

this should get you close to what you want

cat file2 | sort -u | awk '
BEGIN {
while ( getline var file1 > 0 )
if ( var ~ /pattern1/ ) {
x=substr();
}
}}
/pattern2/ {
y=substr();
res= x - y;
if ( res ==2 ) ...
if ( res ==3 ) ...
)
Chris Frangandonis
Regular Advisor

Re: Can AWK read two files

Hi Curt,

Thanks. The only problem is that after the pattern search in both files, I only require the last line for both cases. Only then will I be able to subtract "x-y" and then use result in a "if" loop.

Scot,

My Perl knowledge is about 30%.

Many Thanks
Chris
curt larson_1
Honored Contributor

Re: Can AWK read two files

then something like this

cat file2 | sort -u | awk '
BEGIN {
while ( getline var file1 > 0 )
if ( var ~ /pattern1/ ) {
x=substr();
}
}
/pattern2/ {
y=substr();
}
END {
res= x - y;
if ( res ==2 ) ...
if ( res ==3 ) ...
)