根据部分标题模式删除.csv列

时间:2016-08-17 21:04:30

标签: linux

有没有人知道awk或linux命令根据标题内的部分模式匹配删除.csv文件列?我尝试了很多例子,但它没有做任何事情或者删除了所有列。

Example: I want the columns removed that have the header name "(RAW)" within it.

G_TP01_OPER_ID,G_TP01_OPER_ID(RAW),G_TP02_PROC_NO,G_TP02_PROC_NO(RAW)
4008636142,4008636142,4008636142,4008636142,4008636142

Desired output:

G_TP01_OPER_ID,G_TP02_PROC_NO
4008636142,4008636142

谢谢!

2 个答案:

答案 0 :(得分:0)

awk -F, '{print $1,$3}' OFS="," file

G_TP01_OPER_ID,G_TP02_PROC_NO
4008636142,4008636142

答案 1 :(得分:0)

这是一个shell版本,通过标准输入提供:

read
fields=-f
sep=
i=0
IFS=,
for field in $REPLY
do  # collect fields without "(RAW)"
    ((i=i+1))
    if [[ $field != *"(RAW)"* ]]
    then    # output header name, append field number to $fields
            echo -n "$sep$field"
            fields+="$sep$i" sep=","
    fi
done
echo
cut -d"," "$fields"