如何运行.awk文件? [英] How to run a .awk file?

查看:160
本文介绍了如何运行.awk文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 CSV 文件转换为表格格式,并且编写了AWK脚本并将其保存为my.awk.这是我的脚本:

I am converting a CSV file into a table format, and I wrote an AWK script and saved it as my.awk. Here is the my script:

#AWK for test
awk -F , '
    BEGIN {
        aa = 0;
    }
    {
        hdng = "fname,lname,salary,city";
        l1 = length($1);
        l13 = length($13);
        if ((l1 > 2) &&  (l13 == 0)) {
            fname = substr($1, 2, 1);
            l1 = length($3) - 4;
            lname = substr($3, l1, 4);
            processor = substr($1, 2);
            #printf("%s,%s,%s,%s\n", fname, lname, salary, $0);
        }

        if ($0 ~ ",,,,")
            aa++
        else if ($0 ~ ",fname")
            printf("%s\n", hdng);
        else if ((l1 > 2) && (l13 == 0)) {
            a++;
        }
        else {
            perf = $11;
            if (perf ~/^[0-9\.\" ]+$/)
                type = "num"
            else
                type = "char";
            if (type == "num")
                printf("Mr%s,%s,%s,%s,,N,N,,\n", $0,fname,lname, city);
        }
    }
    END {
    } ' < life.csv > life_out.csv*

如何在Unix服务器上运行此脚本?我试图通过使用以下命令来运行此my.awk文件:

How can I run this script on a Unix server? I tried to run this my.awk file by using this command:

awk -f my.awk life.csv

推荐答案

您提供的文件是 shell脚本,而不是awk程序.因此,尝试sh my.awk.

The file you give is a shell script, not an awk program. So, try sh my.awk.

如果要使用awk -f my.awk life.csv > life_out.cs,请从文件中删除awk -F , '和最后一行,并在BEGIN中添加FS=",".

If you want to use awk -f my.awk life.csv > life_out.cs, then remove awk -F , ' and the last line from the file and add FS="," in BEGIN.

这篇关于如何运行.awk文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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