使用awk按列名获取特定的列号 [英] Get a specific column number by column name using awk

查看:751
本文介绍了使用awk按列名获取特定的列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有n个文件,在这些文件中,每个文件中的不同列号都有一个名为"thrudate"的特定列.

i have n number of file, in these files a specific column named "thrudate" is given at different column number in every files.

i我只想一次从所有文件中提取此列的值.所以我尝试使用awk.在这里,我只考虑一个文件,并提取出thrudate的值

i Just want to extract the value of this column from all files in one go. So i tried with using awk. Here i'm considering only one file, and extracting the values of thrudate

awk -F, -v header=1,head="" '{for(j=1;j<=2;j++){if($header==1){for(i=1;i<=$NF;i++){if($i=="thrudate"){$head=$i;$header=0;break}}} elif($header==0){print $0}}}' file | head -10

我的处理方式:

  • 使用find命令查找所有相似文件,然后对每个文件执行第二步
  • 循环第一行中的所有字段,检查标头值为1的列名(将其初始化为1以仅检查第一行),一旦它与"thrudate"匹配,我将标头设置为0,然后从此循环中断
  • 一旦获得列号,然后为每一行打印一次.

推荐答案

您可以使用以下awk脚本:

You can use the following awk script:

print_col.awk :

# Find the column number in the first line of a file 
FNR==1{
    for(n=1;n<=NF;n++) {
        if($n == header) {
            next
        }
    }
}

# Print that column on all other lines
{
    print $n
}

然后使用find在每个文件上执行此脚本:

Then use find to execute this script on every file:

find ... -exec awk -v header="foo" -f print_col.awk {} +


在注释中,您要求提供一个可以根据其标题名称打印多个列的版本.您可以为此使用以下脚本:


In comments you've asked for a version that could print multiple columns based on their header names. You may use the following script for that:

print_cols.awk :

BEGIN {
    # Parse headers into an assoc array h
    split(header, a, ",")
    for(i in a) {
        h[a[i]]=1
    }   
}

# Find the column numbers in the first line of a file
FNR==1{
    split("", cols) # This will re-init cols
    for(i=1;i<=NF;i++) {
        if($i in h) {
            cols[i]=1
        }
    }   
    next
}

# Print those columns on all other lines
{
    res = ""
    for(i=1;i<=NF;i++) {
        if(i in cols) {
            s = res ? OFS : ""
            res = res "" s "" $i
        }
    }   
    if (res) {
        print res 
    }   
}

这样称呼:

find ... -exec awk -v header="foo,bar,test" -f print_cols.awk {} +

这篇关于使用awk按列名获取特定的列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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