从awk

时间:2018-04-14 06:28:13

标签: awk gawk carriage-return linefeed

我正在使用Windows 7 x64 SP1 cmd.exe中的GNU awk v4.2.0,并遇到以下简单示例的问题:

# calculate the Greatest common divisor of each pair of inputs

BEGIN { RS="[\r\n]" }                                # added this

{
    arg1 =$1; arg2 = $2;

    if ((arg1 == "") || (arg2 == "")) { next }   # and added this

    while (arg1 != arg2)
    {
            if (arg1 > arg2)
                    arg1 -= arg2
            else
                    arg2 -= arg1
    }
    print "The greatest common divisor of", $1, "and", $2, "is", arg1
}

和DOS格式化(CR / LF行结尾)输入文件如下:

75      30
24      60
360    224

数字用空格分隔。原始的awk脚本没有设置RS的BEGIN语句,也没有if语句来测试arg1或arg2是否为空。原始脚本将产生以下输出:

 is 15eatest common divisor of 75 and 30
 is 12eatest common divisor of 24 and 60
 is 8reatest common divisor of 360 and 224

结果(15,12和8)位于错误的位置,因为arg2中存在不需要的回车。

为了解决这个问题,我添加了BEGIN {RS =“[\ r \ n]”},这导致输出为:

The greatest common divisor of 75 and 30 is 15
The greatest common divisor of  and  is
The greatest common divisor of 24 and 60 is 12
The greatest common divisor of  and  is
The greatest common divisor of 360 and 224 is 8
The greatest common divisor of  and  is

这使得GCD的值出现在它应该是的行的末尾但是,它也导致出现不需要/无关的记录。为了摆脱那些我添加了删除它们的if((arg1 ==“”)||(arg2 ==“”)){next}。

这些步骤导致脚本按原样运行,但似乎是解决问题的一种相当复杂的(更不用说不合适的)方式。

我的问题是:是否有更简单,更自然的“awk like”方式让arg2不包含回车符?

0 个答案:

没有答案