使用AWK来连续处理两个不同的文件 [英] Using AWK to process two different files consecutively

查看:89
本文介绍了使用AWK来连续处理两个不同的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用awk连续评估两个文件。在第一个文件的末尾我读日期和我使用的日期作为输入第二个文件的评价。不幸的是我有一些问题理解如何检测第一文件的结尾读出的日期和继续评估下一个文件。我已经找到了一些答案,如FNR == NR,不幸的是,我不能够正确地执行它们。我通过硬编码行数试了穷人的解决方案。然而,这不是做一个非常聪明的事情。我仍然有问题,但在处理第二个文件:

I am trying to evaluate two files consecutively with awk. At the end of the first file I am reading a date and I use that date as input for the evaluation of the second file. Unfortunately I have some problems understanding how to detect the end of the first file read the date and continue evaluating the next file. I have found some answers such as FNR==NR, unfortunately, I am not able to implement them correctly. I tried a poor man’s solution by hardcoding the number of lines. However, this is not a terribly smart thing to do. I still have problems processing the second file though:

    BEGIN initalize the counters 



    {
    if(NR==FNR) <<<<<< this is needed to run properly, only NR==FNR fails, why ?!       
    {     
          # file_1      
          do -> from the last line of the first file extract a date 

          next << what is the meaning of this ??
    }                        

    {
          # file_2
          do -> read every line of the second file 
             and sum up the values form one of the colums


    }


    }


    END { divide the sum accumulated form file=2 
          by the time calculated form the last line of file=1}

# for calling the script use :
awk -f SCRIPT file_1 file_2

#example files
# file1 last line
version 1.5 code 11 mpi start /01/12/2014/ 18:33:12 end /01/12/2014/ 20:05:12

#file2

     1.28371E-05    0.2060    0.2060   -8   -8    0    0    0
     1.91616E-05    0.1927    0.1927   -7   -8    0    0    0
     1.27306E-05    0.1567    0.1567   -6   -8    0    0    0
     2.11623E-05    0.1523    0.1523   -5   -8    0    0    0
     1.67914E-05    0.1721    0.1721   -4   -8    0    0    0
     1.47247E-05    0.1851    0.1851   -3   -8    0    0    0
     1.32049E-05    0.1919    0.1919   -2   -8    0    0    0
     1.81256E-05    0.2130    0.2130   -1   -8    0    0    0
     2.63500E-05    0.1745    0.1745    0   -8    0    0    0
     1.99232E-05    0.1592    0.1592    1   -8    0    0    0
     2.08924E-05    0.1537    0.1537    2   -8    0    0    0
     2.44922E-05    0.1459    0.1459    3   -8    0    0    0
     2.53759E-05    0.1902    0.1902    4   -8    0    0    0
     2.30230E-05    0.1708    0.1708    5   -8    0    0    0
     2.10723E-05    0.1636    0.1636    6   -8    0    0    0
     1.86613E-05    0.1915    0.1915    7   -8    0    0    0
     2.05359E-05    0.1649    0.1649    8   -8    0    0    0
     1.09533E-05    0.1765    0.1765   -8   -7    0    0    0
     1.56917E-05    0.1740    0.1740   -7   -7    0    0    0
     1.52199E-05    0.2145    0.2145   -6   -7    0    0    0
     .....   

我想AP preciate任何帮助,
谢谢你在前进

I would appreciate any help, Thank you in advance

亚历

推荐答案

这听起来像所有你需要的是这样的:

It sounds like all you need is something like:

awk '
NR==FNR {
   do file1 stuff
   date = $0
   next
}
{
   do file2 stuff using the variable "date" which is set to the last line of file1
}
' file1 file2

如果这不是你所需要的,张贴一些样本输入和预期的输出,以帮助澄清你想要做的事。

If that's not all you need, post some sample input and expected output to help clarify what you're trying to do.

这篇关于使用AWK来连续处理两个不同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆