从文件标题匹配的以空格分隔的文件中删除列 [英] Delete columns from space delimited file where file header matches

查看:65
本文介绍了从文件标题匹配的以空格分隔的文件中删除列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以空格分隔的输入文本文件.我想使用sed或awk删除列标题为大小的列.

I have a space delimited input text file. I would like to delete columns where the column header is size using sed or awk.

输入文件:

id quantity colour shape size colour shape size colour shape size
1 10 blue square 10 red triangle 8 pink circle 3
2 12 yellow pentagon 3 orange rectangle 9 purple oval 6

所需的输出:

id quantity colour shape colour shape colour shape
1 10 blue square red triangle pink circle
2 12 yellow pentagon orange rectangle purple oval

推荐答案

awk命令

awk '
NR==1{
    for(i=1;i<=NF;i++)
        if($i!="size")
            cols[i]
}
{
    for(i=1;i<=NF;i++)
        if(i in cols)
            printf "%s ",$i
    printf "\n"
}' input > output


精美打印

column -t -s ' ' output 


结果

id  quantity  colour  shape     colour  shape      colour  shape
1   10        blue    square    red     triangle   pink    circle
2   12        yellow  pentagon  orange  rectangle  purple  oval

这篇关于从文件标题匹配的以空格分隔的文件中删除列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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