AWK - 如何两个脚本互相交流? [英] awk - how two scripts interact with each other?

查看:187
本文介绍了AWK - 如何两个脚本互相交流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有找到有关此主题的任何明确的教程。说我有一个输入文件为:

  1 ABC
1高​​清
1 GHI
1 LALALA
1 heyhey
2 AHB
2 BBH
3 CHCH
3 chchch
3 oiohho
3 nonon
3清真
3什么

说我想找到一个列的最大数量首次出现了,这是3即出现了6次。然后,我就需要这个数(即6)输送到另一个脚本去通过文件做一些计算。什么是做到这一点的方法吗?

基本上,我不知道是否有可能写一个函数去通过文件,找到最大,然后在主函数中调用的辅助函数。另外,我不知道是否有可能做辅助函数中的$(...)来调用'的awk'或其他系统功能?


解决方案

 的awk'NR == FNR {NUMS [$ 1] ++;下一个} !旗旗{= 1;对(在NUMS NUM){如果(NUMS [Ⅰ]≥最大){最大= NUM​​S由[i]}}} {打印最大值* $ 3}filetomax filetoprocess

这是破了多条线路上:

 的awk'
    NR == FNR {
        NUMS [$ 1] ++;
        下一个
    }
    !标志{
        标志= 1;
        对(在NUMS NUM){
            如果(NUMS [Ⅰ]≥最大值){
                最大= NUM​​S [I]
            }
        }
    }
    {
        打印最多* $ 3
    }
filetomax filetoprocess

在这里,我们正在做同样的操作来发现你已 见过的数字的最大。而不是使用一个主块和 END 块,我们使用通常用来处理一个文件,然后另一种技术。同时读取第一个文件,因为该记录号( NR ),这是 NR == FNR 的条件是唯一的真递增中的所有文件的每一行共同地是等于文件记录号( FNR ),其是复位为每个新文件。与此条件相关联的块数显示在每个号码的时间。在接下来语句使执行循环读取文件的下一行。当到达第二个文件,病情不再真实,这个块将被跳过。

下一个条件(!标志)检查是否该变量的内容是真实的。因为它没有被设置,这是错误的。感叹号否定条件,使在这一点上执行移动到该块。现在该标志设置这样的条件被检查接下来的时间,该块将被跳过。在循环检查,看看哪个号码出现的次数最多,在我回答你的其他问题。

现在,第二个文件可以在任何你喜欢的方式处理和变量最大可使用此处理过程中。我只是使用了打印语句来说明这一点。您仍然可以使用块选择条件语句,包括一个或多​​个 END 块,你通常会。我不显示 BEGIN 块,但你可以在此脚本你需要的任何初始化的顶部添加一个。需要注意的是第一个文件的处理可能已在 BEGIN 块是用函数getline 完成。这只是为了完成同样的事情的另一种技术。

的文件名中列出了他们将被处理的顺序。该文件中找到最大计数,我称之为filetomax。第二个文件主做处理上,我称之为filetoprocess。

I am not finding any clear tutorial on this topic. Say I have an input file as:

1 abc
1 def
1 ghi
1 lalala
1 heyhey
2 ahb
2 bbh
3 chch
3 chchch
3 oiohho
3 nonon
3 halal
3 whatever

Say I would like to find the maximum number of column one appeared first, which is "3" that appeared 6 times. Then i will need to feed this number (i.e. 6) to another script to go through the file to do some computations. What are the ways to do this?

Basically, i wonder if it's possible to write a function to go through the file and find "max" then in the main function calling the helper function. Also, i wonder if it's possible to do $(...) within the helper function to call 'awk' or other system functions?

解决方案

awk 'NR == FNR {nums[$1]++; next} ! flag {flag = 1; for (num in nums) {if (nums[i] > max) {max = nums[i]}}} {print max * $3}' filetomax filetoprocess

Here it is broken out on multiple lines:

awk '
    NR == FNR {
        nums[$1]++;
        next
    } 
    ! flag {
        flag = 1; 
        for (num in nums) {
            if (nums[i] > max) {
                max = nums[i]
            }
        }
    } 
    {
        print max * $3
    }
' filetomax filetoprocess

Here, we're doing the same operation to find the max of the numbers that you've seen before. Instead of using a main block and an END block, we're using a technique that's often used to process one file and then another. The NR == FNR condition is only true while the first file is read because the record number (NR) which is incremented for each line in all the files collectively is equal to the file record number (FNR) which is reset for each new file. In the block associated with this condition, count the times each number appears. The next statement causes execution to loop to read the next line from the files. When the second file is reached, the condition is no longer true and this block will be skipped.

The next conditional (! flag) checks to see if the contents of the variable are true. Since it hasn't been set, it's false. The exclamation point negates the condition so at this point execution moves into this block. Now the flag is set so the next time the condition is checked, this block will be skipped. The for loop checks to see which number appeared the most times, as in my answer to your other question.

Now, the second file can be processed in any way you like and the variable max is available for use during this processing. I have simply used a print statement to illustrate that. You can still use block selector conditionals, including one or more END blocks as you normally would. I don't show a BEGIN block, but you could add one at the top of this script for any initialization you need. Note that the processing of the first file could have been done in the BEGIN block using getline. That's simply another technique for accomplishing the same thing.

The filenames are listed in the order they are to be processed. The file to find the maximum counts in I've called "filetomax". The second file to do the main processing on I've called "filetoprocess".

这篇关于AWK - 如何两个脚本互相交流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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