【发布时间】:2023-04-05 11:38:01
【问题描述】:
我想按名称指定一列(即102),找到该列的位置,然后使用cut -5,7- 之类的东西和找到的位置删除指定的列。
这是我的文件头 (delim = "\t"):
#CHROM POS 1 100 101 102 103 107 108
【问题讨论】:
标签: bash
我想按名称指定一列(即102),找到该列的位置,然后使用cut -5,7- 之类的东西和找到的位置删除指定的列。
这是我的文件头 (delim = "\t"):
#CHROM POS 1 100 101 102 103 107 108
【问题讨论】:
标签: bash
这个 awk 应该可以工作:
awk -F'\t' -v c="102" 'NR==1{for (i=1; i<=NF; i++) if ($i==c){p=i; break}; next} {print $p}' file
【讨论】:
cut -d' ' -f1 命令通常只打印单列。
这是一种可能的解决方案,没有只删除一列的限制。它被编写为 bash 函数,其中第一个参数是文件名,其余参数是要排除的列。
rmcol() {
local file=$1
shift
cut -f$(head -n1 "$file" | tr \\t \\n | grep -vFxn "${@/#/-e}" |
cut -d: -f1 | paste -sd,) "$file"
}
如果您想选择而不是排除命名列,请将-vFxn 更改为-Fxn。
这几乎肯定需要某种解释。该函数的前两行只是从参数中删除文件名并将其存储以供以后使用。然后cut 命令将选择适当的列;列号是使用以下复杂管道计算的:
head -n1 "$file" | # Take the first line of the file
tr \\t \\n | # Change all the tabs to newlines [ Note 1]
grep # Select all lines (i.e. column names) which
-v # don't match
F # the literal string
x # which is the complete line
n # and include the line number in the output
"${@/#/-e}" | # Put -e at the beginning of each command line argument,
# converting the arguments into grep pattern arguments (-e)
cut -d: -f1 | # Select only the line number from that matches
paste -sd, # Paste together all the line numbers, separated with commas.
【讨论】:
cut 到--complement 您的选择?
v 很容易:-)
在 bash 中使用 for 循环:
C=1; for i in $(head file -n 1) ; do if [ $i == "102" ] ; then break ; else C=$(( $C + 1 )) ; fi ; done ; echo $C
还有一个完整的脚本
C=1
for i in $(head in_file -n 1) ; do
echo $i
if [ $i == "102" ] ; then
break ;
else
echo $C
C=$(( $C + 1 ))
fi
done
cut -f1-$(($C-1)),$(($C+1))- in_file
【讨论】:
$i == "102" 应该是 $i = "102". ==` 给我这样的错误:stackoverflow.com/questions/2011160/unexpected-operator-error
在不循环列的情况下尝试解决方案,我得到:
#!/bin/bash
pick="$1"
titles="pos 1 100 102 105"
tmp=" $titles "
tmp="${tmp%% $pick* }"
tmp=($tmp)
echo "column ${#tmp[@]}"
如果找不到列名,则会错误地报告最后一列。
【讨论】:
试试这个小的 awk 实用程序来剪切特定的标题 - https://github.com/rohitprajapati/toyeca-cutter
示例用法 -
awk -f toyeca-cutter.awk -v c="col1, col2, col3, col4" my_file.csv
【讨论】: