1

我有一个空格分隔的输入文本文件。我想使用 sed 或 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
4

5 回答 5

6

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
于 2012-07-19T10:29:49.937 回答
3

使用awk. 块中有一个硬编码变量 ( columns_to_delete)BEGIN来指示要删除的字段的位置。然后脚本将计算每个字段的宽度,并删除与变量位置匹配的字段。

假设infile有问题的内容和以下内容script.awk

BEGIN {
    ## Hard-coded positions of fields to delete. Separate them with spaces.
    columns_to_delete = "5 8 11"

    ## Save positions in an array to handle it better.
    split( columns_to_delete, arr_columns )
}


## Process header.
FNR == 1 { 

    ## Split header with a space followed by any non-space character.
    split( $0, h, /([[:space:]])([^[:space:]])/, seps )

    ## Use FIELDWIDTHS to handle fixed format of data. Set that variable with
    ## length of each field, taking into account spaces.
    for ( i = 1; i <= length( h ); i++ ) { 
        len = length( h[i] seps[i] )
        FIELDWIDTHS = FIELDWIDTHS " " (i == 1 ? --len : i == length( h ) ? ++len : len)
    }   

    ## Re-calculate fields with new FIELDWIDTHS variable.
    $0 = $0
}

## Process header too, and every line with data.
{
    ## Flag to know if 'p'rint to output a field.
    p = 1 

    ## Go throught all fields, if found in the array of columns to delete, reset
    ## the 'print' flag.
    for ( i = 1; i <= NF; i++ ) { 
        for ( j = 1; j <= length( arr_columns ); j++ ) { 
            if ( i == arr_columns[j] ) { 
                p = 0 
                break
            }   
        }   

        ## Check 'print' flag and print if set.
        if ( p ) { 
            printf "%s", $i
        }
        else {
            printf " " 
        }
        p = 1 
    }   
    printf "\n"
}

像这样运行它:

awk -f script.awk infile

具有以下输出:

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

编辑哦哦,刚刚意识到输出不正确,因为两个字段之间存在连接。修复这将是太多的工作,因为在开始处理任何内容之前将检查每一行的最大列大小。但是有了这个脚本,我希望你能明白。现在不是时候,也许我可以稍后尝试修复它,但不确定。

编辑 2:固定为删除的每个字段添加额外的空间。这比预期的要容易:-)


编辑3:见评论。

我已经修改了该BEGIN块以检查是否提供了一个额外的变量作为参数。

BEGIN {
    ## Check if a variable 'delete_col' has been provided as argument.
    if ( ! delete_col ) { 
        printf "%s\n", "Usage: awk -v delete_col=\"column_name\" -f script.awk " ARGV[1]
        exit 0
    }   

}

并添加到FNR == 1模式计算要删除的列数的过程:

## Process header.
FNR == 1 { 

    ## Find column position to delete given the name provided as argument.
    for ( i = 1; i <= NF; i++ ) { 
        if ( $i == delete_col ) { 
            columns_to_delete = columns_to_delete " " i
        }   
    }   

    ## Save positions in an array to handle it better.
    split( columns_to_delete, arr_columns )

    ## ...
    ## No modifications from here until the end. Same code as in the original script.
    ## ...
}

现在像这样运行它:

awk -v delete_col="size" -f script.awk infile

结果将是相同的。

于 2012-07-18T12:00:36.000 回答
1

使用cut

$ cut -d' ' -f1-4,6,7,9,10 < in.txt   
id quantity colour shape colour shape colour shape
1 10 blue square red triangle pink circle
2 12 yellow pentagon orange rectangle purple oval
于 2012-07-19T10:25:15.780 回答
0

给定一个固定的文件格式:

cut -f 1-4,6-7,9-10 infile
于 2012-07-18T11:16:17.347 回答
0

如果你有 GNU cut 可用,可以这样做:

columns=$(head -n1 INPUT_FILE \
          | tr ' ' '\n'       \
          | cat -n            \
          | grep size         \
          | tr -s ' '         \
          | cut -f1           \
          | tr -d ' '         \
          | paste -sd ",")

cut --complement -d' ' -f$columns INPUT_FILE

它根据标题生成一个逗号分隔的列表,然后从 INPUT_FILE 中删除该列表的补码。

于 2012-07-19T12:03:45.493 回答