我有一个空格分隔的输入文本文件.我想使用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
Run Code Online (Sandbox Code Playgroud)
期望的输出:
id quantity colour shape colour shape colour shape
1 10 blue square red triangle pink circle
2 12 yellow pentagon orange rectangle purple oval
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
column -t -s ' ' output
Run Code Online (Sandbox Code Playgroud)
id quantity colour shape colour shape colour shape
1 10 blue square red triangle pink circle
2 12 yellow pentagon orange rectangle purple oval
Run Code Online (Sandbox Code Playgroud)